locked
identify sender name or id RRS feed

  • Question

  • User-2024981698 posted

    Version - VS2010

    Sir, In my project I am calling a single event for multiple gridview click. I have to extract some value from gridview column The Problem is that when multiple gridview are calling same event function how to get id of the particular girdview. The coding from my project as as below. ..

       Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged, GridView2.SelectedIndexChanged, GridView3.SelectedIndexChanged
            divdetails.Visible = True
    
            Dim exmno As Integer
            Dim sql As String
           exmno = gridview1.selectedRow.Cells(1).Text

    Pl. help

    thanks

    Wednesday, February 7, 2018 12:21 PM

All replies

  • User475983607 posted

    Try

            protected void TaskGridView_SelectedIndexChanged(object sender, EventArgs e)
            {
                GridView gv = (GridView)sender;
                string Id = gv.ID;
            }

    Wednesday, February 7, 2018 3:03 PM
  • User-1838255255 posted

    Hi bhushan98,

    According to your description and needs, please check the following sample code:

    <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateSelectButton="true">
                    <Columns>
                        <asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="Customer ID" />
                        <asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
                        <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
                        <asp:BoundField ItemStyle-Width="150px" DataField="Country" HeaderText="Country" />
                    </Columns>
                </asp:GridView><br/>
                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateSelectButton="true">
                    <Columns>
                        <asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="Customer ID" />
                        <asp:BoundField ItemStyle-Width="150px" DataField="ContactName" HeaderText="Contact Name" />
                        <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
                        <asp:BoundField ItemStyle-Width="150px" DataField="Country" HeaderText="Country" />
                    </Columns>
                </asp:GridView>
    
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim tblDatas As DataTable = New DataTable
            tblDatas.Columns.Add("CustomerID", GetType(System.String))
            tblDatas.Columns.Add("ContactName", GetType(System.String))
            tblDatas.Columns.Add("City", GetType(System.String))
            tblDatas.Columns.Add("Country", GetType(System.String))
            tblDatas.Rows.Add("1", "2", "3", "4")
            tblDatas.Rows.Add("1", "2", "3", "4")
            tblDatas.Rows.Add("1", "2", "3", "4")
            tblDatas.Rows.Add("1", "2", "3", "4")
            GridView1.DataSource = tblDatas
            GridView1.DataBind()
            Dim dt As DataTable = New DataTable
            dt.Columns.Add("CustomerID", GetType(System.String))
            dt.Columns.Add("ContactName", GetType(System.String))
            dt.Columns.Add("City", GetType(System.String))
            dt.Columns.Add("Country", GetType(System.String))
            dt.Rows.Add("1", "2", "3", "4")
            dt.Rows.Add("1", "2", "3", "4")
            dt.Rows.Add("1", "2", "3", "4")
            dt.Rows.Add("1", "2", "3", "4")
            GridView2.DataSource = dt
            GridView2.DataBind()
        End Sub
    
        Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs)
            Dim gv As GridView = CType(sender, GridView)
            Dim Id As String = gv.ID
        End Sub

    Best Regards,

    Eric Du

    Friday, February 9, 2018 10:20 AM