Results 1 to 4 of 4

Thread: [2008] GridView Selected Value

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    262

    [2008] GridView Selected Value

    How can I determine exactly which cell someone clicked in a GridView? I have 2 columns in my GridView that I need to fire an event. Anyone have a sample of how this could work?

  2. #2
    Junior Member
    Join Date
    Feb 2009
    Posts
    26

    Smile Re: [2008] GridView Selected Value

    Hi,

    I can give you some sample code here. Open a default web site in VS 2005 and copy the code given below for Default.aspx and Default.aspx.cs

    Code for Default.aspx

    Code:
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:TextBox ID="mytextbox" runat="server" style="z-index: 100; left: 141px; position: absolute; top: 18px" Width="647px"></asp:TextBox>
        <asp:DataGrid ID="newgrid" runat="server" AutoGenerateColumns="False" style="z-index: 102; left: 212px; position: absolute; top: 100px" Width="504px">
            <Columns>
                <asp:TemplateColumn>
                    <ItemTemplate>
                        <asp:CheckBox AutoPostBack="true" EnableViewState="true" OnCheckedChanged="CheckBox1_Click" ID="CheckBox1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Col1") %>' />
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn>
                    <ItemTemplate>
                        <asp:CheckBox AutoPostBack="true" EnableViewState="true" OnCheckedChanged="CheckBox2_Click" ID="CheckBox2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Col2") %>' />
                    </ItemTemplate>
                </asp:TemplateColumn>
                
            </Columns>
        </asp:DataGrid>
        </div>
        </form>
    </body>
    </html>
    Code for Default.aspx.cs

    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                mytextbox.Text = "You have not clicked on any of the check boxes.";
                DataSet ds = new DataSet();
                DataTable dt = new DataTable("MyTable");
                dt.Columns.Add("Col1", System.Type.GetType("System.String"));
                dt.Columns.Add("Col2", System.Type.GetType("System.String"));
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                dt.Rows[0]["Col1"] = "Click on me. I am Check Box number 1";
                dt.Rows[0]["Col2"] = "Click on me. I am Check Box number 2";
                ds.Tables.Add(dt);
                newgrid.DataSource = ds;
                newgrid.DataBind();
            }
        }
        protected void CheckBox1_Click(object sender, EventArgs e)
        {
            mytextbox.Text = "You clicked on check box number 1";
        }
        protected void CheckBox2_Click(object sender, EventArgs e)
        {
            mytextbox.Text = "You clicked on check box number 2";
        }
    
    }
    The application simply creates a Data set containing a data table with 2 columns Col1 and Col2. There is a data grid which has 2 check boxes bound to the 2 columns of the data table that is created. The text for the two check boxes are the strings in the two columns. There is also a text box. If you run the page then initially this text box says

    "You have not clicked on any of the check boxes."

    And a data grid appears with 2 check boxes. Click on any one of the check boxes and the page will post back and the text box will tell you on which check box you clicked. Mind you, it does not tell you whether the check box was checked or unchecked. It just tells you on which check box you clicked most recently.

    This sample is just to demonstrate just how you can get controls inside a data grid or a grid view to fire events immediately and also how you can trap those events.

    I am not clear about exactly what you require. I am just giving this sample thinking it maybe what you need.

    Warm Regards ,
    sr_jay

    Quote Originally Posted by ooOOJaVaOOoo
    How can I determine exactly which cell someone clicked in a GridView? I have 2 columns in my GridView that I need to fire an event. Anyone have a sample of how this could work?

  3. #3
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [2008] GridView Selected Value

    Quote Originally Posted by ooOOJaVaOOoo
    How can I determine exactly which cell someone clicked in a GridView? I have 2 columns in my GridView that I need to fire an event. Anyone have a sample of how this could work?
    LinkButtons are the ideal control to use in GridViews, in my opinion, as they offer the greatest flexibility. There are two situations possible from your example, and I think I know which one you're getting at. GridViews do have the ability to "select" rows, but I don't think that's what you're going for so much as enabling custom functionality. For this, create a LinkButton control, give it the ID and runat="server", then specify CommandName and CommandArgument. In the GridView's RowCommand event, check e.CommandName to see which link button was pressed (so naturally, the two link buttons will have different CommandNames), and e.CommandArgument to get the data stored there to use. I typically use that data item's ID in the database as the CommandArgument, as I can throw that at a stored procedure with no thought.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: [2008] GridView Selected Value

    I'd go with CommandName and CommandArgument too. It lets you keep all your logic on one method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width