Click here to Skip to main content
15,883,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make gridview cell clickable so as to get the value from the gridview label but what i am currently doing is not working.This is what i have done so far.

ASP.NET
<asp:GridView ID="gvweeklysum" runat="server" CellPadding="4" ForeColor="#333333"
      GridLines="None" Width="465px" AutoGenerateColumns="False" OnRowDataBound="gvweeklysum_RowDataBound">
       <alternatingrowstyle backcolor="White" forecolor="#284775" />
          <editrowstyle backcolor="#999999" />
          <footerstyle backcolor="#5D7B9D" font-bold="True"/>
      <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
      <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
       <pagerstyle backcolor="#284775" forecolor="White" HorizontalAlign="Center"/>
     <rowstyle backcolor="#F7F6F3" forecolor="#333333" />
          <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
          <sortedascendingcellstyle backcolor="#E9E7E2" />
          <sortedascendingheaderstyle backcolor="#506C8C" />
          <sorteddescendingcellstyle backcolor="#FFFDF8" />
          <sorteddescendingheaderstyle backcolor="#6F8DAE" />
      <Columns>
          <asp:BoundField DataField="s008_Weekending" HeaderText="Weekending" InsertVisible="False"
              ReadOnly="True" SortExpression="" />
          <asp:BoundField DataField="s008_hrsday1" HeaderText="Monday" InsertVisible="False"
              ReadOnly="True" SortExpression="" />
          <asp:BoundField DataField="s008_hrsday2" HeaderText="Tuesday" InsertVisible="False"
              ReadOnly="True" SortExpression="" />
          <asp:BoundField DataField="s008_hrsday3" HeaderText="Wednesday" InsertVisible="False"
              ReadOnly="True" SortExpression="" />
          <asp:BoundField DataField="s008_hrsday4" HeaderText="Thursday" InsertVisible="False"
              ReadOnly="True" SortExpression="" />
          <asp:BoundField DataField="s008_hrsday5" HeaderText="Friday" InsertVisible="False"
              ReadOnly="True" SortExpression="" />
          <asp:BoundField DataField="s008_hrsday6" HeaderText="Saturday" InsertVisible="False"
              ReadOnly="True" SortExpression="" />
          <asp:BoundField DataField="s008_hrsday7" HeaderText="Sunday" InsertVisible="False"
              ReadOnly="True" SortExpression="" />
          <asp:TemplateField HeaderStyle-VerticalAlign="Middle">
              <ItemTemplate>
                  <asp:Label ID="lblweekending" runat="server" Text='<%# Eval("s008_Weekending") %>' />
                  <asp:Label ID="lblmonday" runat="server" Text='<%# Eval("s008_hrsday1") %>' />
                  <asp:Label ID="lbltuesday" runat="server" Text='<%Eval("s008_hrsday2") %>' />
                  <asp:Label ID="lblwednesday" runat="server" Text='<%Eval("s008_hrsday3") %>' />
                  <asp:Label ID="lblthursday" runat="server" Text='<%Eval("s008_hrsday4") %>' />
                  <asp:Label ID="lblfriday" runat="server" Text='<%Eval("s008_hrsday5") %>' />
                  <asp:Label ID="lblsaturday" runat="server" Text='<%Eval("s008_hrsday6") %>' />
                  <asp:Label ID="lblsunday" runat="server" Text='<%Eval("s008_hrsday8") %>' />
              </ItemTemplate>
          </asp:TemplateField>
      </Columns>
  </asp:GridView>
Posted
Updated 8-Jul-13 19:48pm
v2
Comments
joshrduncan2012 8-Jul-13 17:59pm    
Why isn't it working? What error are you getting? Saying "it's not working" doesn't help us.
Amit Karyekar 8-Jul-13 20:52pm    
I am not getting any errors.The binding is done properly showing the data.Want i wanted is that it should fire an event when i click the cell in the gridview.

Asp_Learner 8-Jul-13 19:13pm    
Are you binding it by c# or SqlDataSource ?
Amit Karyekar 8-Jul-13 20:52pm    
i am binding it by C#
Chinmaya C 9-Jul-13 0:08am    
You can use javascript to raise server call back event...

or else add button to the grid cell instead of label... and set css style of the button so that it will look like label.

I think it would be better if you implement template column and in the template column you need to use required control. Finally it needs to implement RowDataBound

XML
protected void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
 {

   if(e.Row.RowType == DataControlRowType.DataRow)
   {
     // Display the company name in italics.
     e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";

   }

 }


Thanks,
Mamun
 
Share this answer
 
In gvweeklysum_RowDataBound, add click event for each of the cell...
something like this
C#
protected void gvweeklysum_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          //Check as data
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              //add a click event handler
              //This will add one alert to to the first cell
              e.Row.Cells[0].Attributes.Add("onClick", "alert('Cell 0 Clicked');");
              e.Row.Cells[1].Attributes.Add("onClick", "alert('Cell 1 Clicked');");
          }
      }
 
Share this answer
 
C#
protected void gvweeklysum_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
            string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");

            for (int columnIndex = 0; columnIndex < e.Row.Cells.Count; columnIndex++)
            {

                string js = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString());

                e.Row.Cells[columnIndex].Attributes["onclick"] = js;

                e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;";
            }
        }
    }
}
 
Share this answer
 
Comments
Amit Karyekar 9-Jul-13 6:20am    
This worked out for me.Thanks everyone for responding and making it simple.Cheers!!!!
Amit Karyekar 9-Jul-13 6:27am    
Another thing which i need is to bind another gridview based on the cell value clicked.There are two events row databound and rowcommand.Where the code must be written in order to bind another gridview?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900