GridView: Adding confirmation before delete with imagebutton

<asp:templatefield HeaderText="" ShowHeader="False">

<itemtemplate>

<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" OnClientClick="return confirm(‘Are you sure you want to delete this record?’);" CommandName="Delete" ImageUrl="images/icon-delete.gif" Text="Delete" />

</itemtemplate>

<EditItemTemplate></EditItemTemplate>

</asp:templatefield>

Refer: http://outside-world.spaces.live.com/Blog/cns!1pAcnUvTRdw34Pi408FNX3ag!127.entry


gridview column to display hh:mm:ss from an integer field

 Assume the mySeconds column is the first column in the Gridview and with a data type int in the table. You can change the index accordingly with your need. Here is the code: 
 
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
            DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
            <asp:BoundField DataField="mySeconds" HeaderText="mySeconds"  />
................................................
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DateTime mydt = DateTime.Parse("01/01/2007");

            Double myDouble=Convert.ToDouble((Int32)DataBinder.Eval(e.Row.DataItem, "mySeconds"));

            e.Row.Cells[0].Text = mydt.AddSeconds(myDouble).ToString("hh:mm:ss");
      }
}

Highlight a row from GridView without the built-in SELECT

 
1. Create a style for the highlight and no show:

<style type="text/css">

.SelectedRowStyle {background-color: yellow;}

.Noshow{display:none;}

</style>

2.

2.1 And add the style to the griview:  SelectedRowStyle-CssClass="SelectedRowStyle"

2.2 And add the noshow style to the SELECT Commandfield:

<asp:CommandField SelectText ="Select" ShowSelectButton="true" ItemStyle-CssClass = "Noshow" HeaderStyle-CssClass ="Noshow" />

3. Register a postback javascript through OnRowDataBound event:

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)

        if (e.Row.RowType == DataControlRowType.DataRow) {
            e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex.ToString()));
        } 
 
}


Add tooptip/title to a sortable gridview header

Find the linkbuttons from the header row and assign the text to them throgh OnRowDataBound event.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
          
            foreach (System.Web.UI.WebControls.TableCell cell in e.Row.Cells)
            {
                foreach (Control ctl in cell.Controls)
                {
                    LinkButton link = ctl as LinkButton;
                    if (link != null)
                    {
                       link.ToolTip="By clicking here you will sort this column ";
                    }
                   
                }
            }
        }
    }