Why can’t i change the value in a specific row in the rowdatabound event of my gridview? the code is entering where the value is set to Test but still shows old value?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="au_id" DataSourceID="SqlDataSource1"
ondatabinding="GridView1_DataBinding" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True"
SortExpression="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname"
SortExpression="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="au_fname"
SortExpression="au_fname" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT [au_id], [au_lname], [au_fname] FROM [authors]">
</asp:SqlDataSource>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var row = ((DataRowView)e.Row.DataItem).Row;
var customerName = row.Field<String>("au_lname");
if (customerName == "Carson")
{
customerName = "Test";
}
}
}
Because you cannot change the underlying DataSource in
RowDataBound(too late). You need to apply your changes to theTemplateFieldscontrols or to theCellCollectionof the row(in case ofBoundFieldsorAutogenerateColumns=true):