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!
I saw that your DataKeyNames setting in GridView control is like this
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:
And you should also add this column:
Did you try that?
In other way, you could try to use TemplateField instead
And this code to get data from prID column in GridView1_RowUpdating event handler:
Sorry if this doesn't help.