[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 764
  • Last Modified:

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

0
kxradhi
Asked:
kxradhi
  • 5
  • 4
  • 2
1 Solution
 
kxradhiAuthor Commented:
I have a gridview for which the datasource is a dataset. In the  rowediting event, i have

        GridView2.EditIndex = e.NewEditIndex;

In the row update event , i have the following -
public void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        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_Page(t1,t);
}
and the definition for gridview -
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="true"
              BorderColor="#FFFF80" Font-Size="Small"  OnRowEditing="GridView2_RowEditing"
              onselectedindexchanged="GridView2_SelectedIndexChanged"
              OnUpdateCommand="GridView2_RowUpdating" OnCancelCommand="GridView2_RowCancelingEdit"  
              Width="264px" onrowupdating="GridView2_RowUpdating" >
              <Columns>
              <asp:CommandField
                       ShowEditButton="True"></asp:CommandField>
              </Columns>
          </asp:GridView>
I am getting an error - System.NullReferenceException: Object reference not set to an instance of an object.

Please help?
0
 
Bob LearnedCommented:
It would help to know what line you are getting that exception with?
0
 
kxradhiAuthor Commented:
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;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Bob LearnedCommented:
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.
0
 
prairiedogCommented:
Hi kxradhi,
You are on the right track.
In Edit mode, all editable fields become TextBoxes, so first you need to find those text boxes before you can retrieve their values.
string t = (string)row.Cells[1].Text; is not the right way to retrieve data. Change it to this:
TextBox txt1 = (TextBox)row.Cells[1].Controls[0];
TextBox txt2 = (TextBox)row.Cells[0].Controls[0];
string t = txt1.Text;
string t1 = txt2.Text;
Then continue with your update process.
0
 
kxradhiAuthor Commented:
I changed the code to use the textboxes and I am getting this error
Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.
0
 
kxradhiAuthor Commented:
Do i have to create text boxes and bind it?
0
 
prairiedogCommented:
You will need to change the index in Cells[i] to match the index where the textbox is located.
0
 
Bob LearnedCommented:
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



<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>

Open in new window

0
 
kxradhiAuthor Commented:
ok. I  have added the TemplateFields but I am still getting an error - DataBinding: 'System.Data.Common.DataRecordInternal' 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?
0
 
Bob LearnedCommented:
Can you show us what you came up with, please?
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now