locked
Get a cell index from selected row RRS feed

  • Question

  • User-1309838614 posted

    Hello, I want to know how to get a cell index from selected row in GridView. 

    I created the table which user can click any rows. 

            Dim index As Integer = GridView12.SelectedRow.RowIndex 'this get the row index
            Dim name As String = GridView12.SelectedRow.Cells(0).Text
            Dim station As String 'get the row header column name from cell index
            Dim message As String = "Row Index: " & index & "\nName: " & name + "\nStation: " & station
            ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('" + message + "');", True)

    Thank you. 

    Edit:

    The result is gonna be like this picture

     

    This is my real table, if I want to click on the red box, the result gonna be Row: T001, Cells: Station 01.

    I am sorry for my confused explanation.

    Thursday, September 28, 2017 10:07 AM

Answers

  • User-943250815 posted

    oliviaolda15

    All suggestions are ok, for checkbox use as old picture. Also your code call SelectedRow, which restrict you to a specific row and specific cell, not any row/cell
    With new picture, seems you want to pick any row/cell.
    To achieve this working model, I suggest use Gridview.RowCommand. You have to make some changes in your grid but it will work as (I think) you want.
    With some CSS adjustments you can have linkbutton show as text only

    Gridview (attention columns should be Template)

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="LinqDataSource3">
        <Columns>
          <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" />
          <asp:TemplateField HeaderText="Station 01">
            <ItemTemplate>
              <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="Station 01" CommandName="WhatCell" Text='<%# Bind("Station01") %>'></asp:LinkButton>
            </ItemTemplate>
          </asp:TemplateField>
                <asp:TemplateField HeaderText="Station 02">
            <ItemTemplate>
              <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="Station 02" CommandName="WhatCell" Text='<%# Bind("Station02") %>'></asp:LinkButton>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Station 03">
            <ItemTemplate>
              <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument="Station 03" CommandName="WhatCell" Text='<%# Bind("Station03") %>'></asp:LinkButton>
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>

    Code behind

    Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "WhatCell" Then
          Dim ColumnText = e.CommandArgument
          Dim gvr As GridViewRow = CType(e.CommandSource, LinkButton).NamingContainer
          Dim RowText = gvr.Cells(0).Text
          ' Here you do whatever you want with ColumnText & RowText
        End If
      End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 30, 2017 4:11 PM

All replies

  • User-943250815 posted

    If you have cell number use GridView12.HeaderRow.Cells(cellnumer).Text

    Thursday, September 28, 2017 1:54 PM
  • User-1309838614 posted

    How about if user wants to selected any cells, how computer detect where is the position of the selected cells? The row and column position?

    Friday, September 29, 2017 1:06 AM
  • User-943250815 posted

    The only way I see is loop through each cell, find checkbox control then get if it is checked or not

    Friday, September 29, 2017 1:00 PM
  • User-335504541 posted

    Hi olivaiaolga15,

    Please try to use the follow code:

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
            Dim index As Integer = GridView1.SelectedRow.RowIndex 'this get the row index
            Dim cindex As Integer = 0
            Dim name As String = GridView1.SelectedRow.Cells(0).Text
    
            For Each cell As TableCell In GridView1.SelectedRow.Cells
                Dim CheckBox As CheckBox = TryCast(cell.Controls(1), CheckBox)
                If CheckBox IsNot Nothing Then
                    If CheckBox.Checked Then
    
                        Dim message As String = "Row Index: " & index & "\nName: " & name + "\nStation: " & station
                        'cindex  is the index
                    End If
                End If
                cindex = cindex + 1
            Next
    
    
        End Sub

    Best Regards,

    Billy

    Friday, September 29, 2017 2:16 PM
  • User2103319870 posted

    I want to know how to get a cell index from selected row in GridView

    You can use  GetCellIndex Method to get the cellindex from selectedrow in gridview like below

                        'You can find cell index like below
                        Dim cellindex As Int32 = GridView1.SelectedRow.Cells.GetCellIndex(cell)

    Complete Code

      Protected Sub Button1_Click(sender As Object, e As EventArgs)
            Dim index As Integer = GridView1.SelectedRow.RowIndex 'this get the row index
            Dim cindex As Integer = 0
            Dim name As String = GridView1.SelectedRow.Cells(0).Text
    
            For Each cell As TableCell In GridView1.SelectedRow.Cells
                Dim CheckBox As CheckBox = TryCast(cell.Controls(1), CheckBox)
                If CheckBox IsNot Nothing Then
                    If CheckBox.Checked Then
    
                        Dim message As String = "Row Index: " & index & "\nName: " & name + "\nStation: " & station
                        'cindex  is the index
                        'You can find cell index like below
                        Dim cellindex As Int32 = GridView1.SelectedRow.Cells.GetCellIndex(cell)
                    End If
                End If
                cindex = cindex + 1
            Next
        End Sub

     

    Friday, September 29, 2017 3:05 PM
  • User-1309838614 posted

    HI, I want to ask what is the purpose of cindex if to get the cell index is from getcellindex? I still don't get it. I have tried that but it is not working.

    Saturday, September 30, 2017 9:24 AM
  • User2103319870 posted

    I want to ask what is the purpose of cindex if to get the cell index is from getcellindex?

    You don't need both, you can use one form the two. I just used the code provided by previous poster as a sample so that you will understand where we should use the code to find cell index using getCellIndex method

    Saturday, September 30, 2017 3:48 PM
  • User-943250815 posted

    oliviaolda15

    All suggestions are ok, for checkbox use as old picture. Also your code call SelectedRow, which restrict you to a specific row and specific cell, not any row/cell
    With new picture, seems you want to pick any row/cell.
    To achieve this working model, I suggest use Gridview.RowCommand. You have to make some changes in your grid but it will work as (I think) you want.
    With some CSS adjustments you can have linkbutton show as text only

    Gridview (attention columns should be Template)

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="LinqDataSource3">
        <Columns>
          <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" />
          <asp:TemplateField HeaderText="Station 01">
            <ItemTemplate>
              <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="Station 01" CommandName="WhatCell" Text='<%# Bind("Station01") %>'></asp:LinkButton>
            </ItemTemplate>
          </asp:TemplateField>
                <asp:TemplateField HeaderText="Station 02">
            <ItemTemplate>
              <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="Station 02" CommandName="WhatCell" Text='<%# Bind("Station02") %>'></asp:LinkButton>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Station 03">
            <ItemTemplate>
              <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument="Station 03" CommandName="WhatCell" Text='<%# Bind("Station03") %>'></asp:LinkButton>
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>

    Code behind

    Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "WhatCell" Then
          Dim ColumnText = e.CommandArgument
          Dim gvr As GridViewRow = CType(e.CommandSource, LinkButton).NamingContainer
          Dim RowText = gvr.Cells(0).Text
          ' Here you do whatever you want with ColumnText & RowText
        End If
      End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 30, 2017 4:11 PM
  • User-1309838614 posted

    Yes! It works! Thank you @jzero

    jzero

    oliviaolda15

    All suggestions are ok, for checkbox use as old picture. Also your code call SelectedRow, which restrict you to a specific row and specific cell, not any row/cell
    With new picture, seems you want to pick any row/cell.
    To achieve this working model, I suggest use Gridview.RowCommand. You have to make some changes in your grid but it will work as (I think) you want.
    With some CSS adjustments you can have linkbutton show as text only

    Gridview (attention columns should be Template)

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="LinqDataSource3">
        <Columns>
          <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" />
          <asp:TemplateField HeaderText="Station 01">
            <ItemTemplate>
              <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="Station 01" CommandName="WhatCell" Text='<%# Bind("Station01") %>'></asp:LinkButton>
            </ItemTemplate>
          </asp:TemplateField>
                <asp:TemplateField HeaderText="Station 02">
            <ItemTemplate>
              <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="Station 02" CommandName="WhatCell" Text='<%# Bind("Station02") %>'></asp:LinkButton>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Station 03">
            <ItemTemplate>
              <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument="Station 03" CommandName="WhatCell" Text='<%# Bind("Station03") %>'></asp:LinkButton>
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>

    Code behind

    Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "WhatCell" Then
          Dim ColumnText = e.CommandArgument
          Dim gvr As GridViewRow = CType(e.CommandSource, LinkButton).NamingContainer
          Dim RowText = gvr.Cells(0).Text
          ' Here you do whatever you want with ColumnText & RowText
        End If
      End Sub

    Monday, October 2, 2017 1:55 AM