Search code examples
c#asp.netgridviewcode-behindreadonly

How to access a selected Boundfield value from a GridView in code-behind


I have seen similar questions but none of the answers helped me to workout this problem. I have GridView with a ReadOnly field as follow.

GridView:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
              AutoGenerateColumns="False" DataKeyNames="projectID" 
              DataSourceID="SqlDataSource1" 
              EmptyDataText="There are no data records to display." 
              PageSize="5" OnRowUpdating="GridView1_RowUpdating">
  <Columns>
    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"/>
    <asp:BoundField DataField="prID" HeaderText="prID" SortExpression="prID"/>
    <asp:BoundField DataField="projectName" HeaderText="projectName" 
                    SortExpression="projectName" />
    <asp:BoundField DataField="projectType" HeaderText="projectType" 
                    SortExpression="projectType" />
  </Columns>
  <EditRowStyle CssClass="GridViewEditRow"/>
</asp:GridView>

as you can see the prID BoundField has Readonly=True attribute. I'm trying to get the value of the prID in code-behind when user is updating the other fields in the row.

code-behind:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    GridViewRow row = GridView1.Rows[e.RowIndex];

    String d1 = ((TextBox)(row.Cells[2].Controls[0])).Text;
    String d2 = ((TextBox)(row.Cells[3].Controls[0])).Text;

    // this only works while the field is not readonly      
    string prIDUpdate = ((TextBox)(row.Cells[1].Controls[0])).Text; 

}

Note: I have tried using GridView1.DataKeys[e.RowIndex] and also onRowDataBound an d setting the BoundField ready only in code-behind but I haven't been able to get results

Thanks in advance!


Solution

  • I saw that your DataKeyNames setting in GridView control is like this

    DataKeyNames="projectID"
    

    Then I guess that your key name is projectID not prID, isn't it? If so, you could get data for the selected row as this line:

    string id = GridView1.DataKeys[e.RowIndex]["projectID"].ToString();
    

    And you should also add this column:

    <asp:BoundField DataField="projectID" HeaderText="prID" SortExpression="projectID"/>
    

    Did you try that?

    In other way, you could try to use TemplateField instead

    <Columns>
                <asp:TemplateField HeaderText="prID" SortExpression="prID">
                    <ItemTemplate>
                        <asp:Label ID="lblPrId" runat="server" Text='<%# Bind("prID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="projectName" HeaderText="projectName" 
                        SortExpression="projectName" />
                <asp:BoundField DataField="projectType" HeaderText="projectType" 
                        SortExpression="projectType" />
      </Columns>
    

    And this code to get data from prID column in GridView1_RowUpdating event handler:

    Label lblPrId = row.FindControl("lblPrId") as Label;    
    string prId = lblPrId .Text;
    

    Sorry if this doesn't help.