Solved

Updating a gridview populated with a datatable

Posted on 2007-03-20
6
806 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 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