Question How to get selected row data from grid

raysefo

Well-known member
Joined
Jul 21, 2010
Messages
207
Programming Experience
Beginner
Hi,

I m new to VB.NET. I would like to know, if its possible to get selected datas from a grid?

Thanks in advance.

Best regards.
 
GridView How to get selected row value???

Hi,

I have a gridview like below, and i would like to get value of A. How can i get this value in code behind?

thanks in advance.

Best regards

VB.NET:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
                            EmptyDataText="There are no data records to display." AllowPaging="True" AllowSorting="True"
                            CellPadding="4" ForeColor="#333333" GridLines="None" Style="margin-right: 0px"
                            Width="422px" DataKeyNames="EformID,EformType,TaskID">
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            <Columns>
                                <asp:BoundField DataField="A" HeaderText="Talebi Yapan" SortExpression="A">
                                    <ItemStyle Font-Bold="True" ForeColor="Red" HorizontalAlign="Center" />
                                </asp:BoundField>
                                <asp:BoundField DataField="B" HeaderText="Talep Zamanı" SortExpression="B"
                                    ReadOnly="True">
                                    <ItemStyle Font-Bold="True" HorizontalAlign="Center" />
                                </asp:BoundField>
                                                              
                            </Columns>
 
I would add a select button e.g.

PHP:
<asp:ButtonField CommandName="Select" Text="Select" />

Then:
VB.NET:
     Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Select" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = GridView1.Rows(index)
            Dim TalebiYapan As String = row.Cells(0).Text
            Dim TalepZamanı As String = row.Cells(1).Text
        End If
    End Sub
 
How can i get value if the column (BoundField) is hidden? I mean, i can add this field into DataKeyNames and get value like below;

Dim rowIndex As Int32 = Convert.ToInt32(e.CommandArgument.ToString())
formID = GridView1.DataKeys(rowIndex).Values("name").ToString()

But i would like to know if there is a way to get value of hidden columns without using DataKeyNames??

thanks.
 
No you can't get value of the databind fields that aren't visible but as you already know you can access the value through the DataKeys property!
However, you can use TemplateField for this :)

PHP:
           <asp:TemplateField HeaderText="Talebi Yapan" Visible="false">
                <ItemTemplate>
                    <asp:Label runat="server" ID="TalebiYapan" Text='<%# Eval("DBField") %>' />
                </ItemTemplate>
            </asp:TemplateField>
 
Back
Top