kxradhi
asked on
GridView Update Rows
Hello,
I am trying to enable edit/update for a gridview(has 2 columns). I am able to edit it, but not able to update it.I have a procedure that takes 2 params a,b. I want to pass the values of the edited row in the gridview to the procedure to update.Any help please?
Thanks
I am trying to enable edit/update for a gridview(has 2 columns). I am able to edit it, but not able to update it.I have a procedure that takes 2 params a,b. I want to pass the values of the edited row in the gridview to the procedure to update.Any help please?
Thanks
It would help to know what line you are getting that exception with?
ASKER
Actually i was able to solve the issue of null reference, but i am not able to get the new values of the edited columns, The update procedure is working fine.
string t = (string)row.Cells[1].Text;
string t1 = (string)row.Cells[0].Text;
string t = (string)row.Cells[1].Text;
string t1 = (string)row.Cells[0].Text;
What did you do to fix the exception, and what are you trying to achieve with the GridView that is doesn't do now? I don't understand what those two lines are supposed to be doing for you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I changed the code to use the textboxes and I am getting this error
Unable to cast object of type 'System.Web.UI.WebControls .DataContr olLinkButt on' to type 'System.Web.UI.WebControls .TextBox'.
Unable to cast object of type 'System.Web.UI.WebControls
ASKER
Do i have to create text boxes and bind it?
You will need to change the index in Cells[i] to match the index where the textbox is located.
Yes, if you want to edit something, you need to create TemplateField columns, and have an ItemTemplate with Label controls, and an EditItemTemplate for TextBox controls. Then, you can use row.FindControl with the ID for the control to get a reference.
Example shown here:
Using TemplateFields in the GridView Control
http://www.asp.net/learn/data-access/tutorial-12-cs.aspx
Example shown here:
Using TemplateFields in the GridView Control
http://www.asp.net/learn/data-access/tutorial-12-cs.aspx
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
ASKER
ok. I have added the TemplateFields but I am still getting an error - DataBinding: 'System.Data.Common.DataRe cordIntern al' does not contain a property with the name 'Role_Id'. I am using a dataset to retrieve the rows into a gridview. Am i missing something?
Can you show us what you came up with, please?
ASKER
GridView2.EditIndex = e.NewEditIndex;
In the row update event , i have the following -
public void GridView2_RowUpdating(obje
{
Create_Role create_role = new Create_Role();
GridViewRow row = GridView2.Rows[e.RowIndex]
string t = (string)row.Cells[1].Text;
string t1 = (string)row.Cells[0].Text;
create_role.Update_Role_Pa
}
and the definition for gridview -
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="true"
BorderColor="#FFFF80" Font-Size="Small" OnRowEditing="GridView2_Ro
onselectedindexchanged="Gr
OnUpdateCommand="GridView2
Width="264px" onrowupdating="GridView2_R
<Columns>
<asp:CommandField
ShowEditButton="True"></as
</Columns>
</asp:GridView>
I am getting an error - System.NullReferenceExcept
Please help?