Solved

Updating a gridview populated with a datatable

Posted on 2007-03-20
6
802 Views
Last Modified: 2012-05-05
I have a page that has the gridview below. I am populating this gridview with a dataTable. I run a stored procedure
 that performs a search. I will like to update any row once my result is displayed in the grid.
 How can I do that?
 Here is my gridview below:


td>
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateEditButton="true" DataKeyNames="staffid" >
         <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
         <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
         <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
         <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
         <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
         <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
         <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
</td>


  Here is my asp.net code
 
           SqlDataAdapter da = new SqlDataAdapter("SamplePerformSearch", cn);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.Add(new SqlParameter("@StaffID", SqlDbType.VarChar, 4));
            da.SelectCommand.Parameters["@StaffID"].Value = txtStaffID.Text;

            da.SelectCommand.Parameters.Add(new SqlParameter("@LastName", SqlDbType.VarChar, 30));
            da.SelectCommand.Parameters["@LastName"].Value = txtLastName.Text;

            da.SelectCommand.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.VarChar, 30));
            da.SelectCommand.Parameters["@FirstName"].Value = txtFirstName.Text;

            DataTable dt = new DataTable();
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
   
  my search result only returns staffid, lastname, firstname, address.
  I want to be able to Edit and update lastname,firstname and address from the gridview.

Thanks
0
Comment
Question by:Sirdots
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
G0ggy earned 500 total points
ID: 18757427
Have a read of 4Guys article on the datagrid, it details everything you want to do over the 18 parts:

http://aspnet.4guysfromrolla.com/articles/040502-1.aspx
0
 

Author Comment

by:Sirdots
ID: 18757550
Thanks Goggy.  I am using a gridview and not a datagrid. [visual studio 2005]
0
 
LVL 11

Expert Comment

by:G0ggy
ID: 18757600
I know mate, but a lot of the properties and events are the same/similar, and their article is comprehensive, it should help you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now