Just want to confirm that whether your problem get solved?Any question or feedback,Welcome your info……
PS:Yes,you can handle gridview's events by re-writing them,and I'd like to show this example done by me about "How to do batch update with the help of GridView in the form of Excel-Like thing……",You can download it freely to see the original codes……
Hello Decker Dong!! It didnt work for me.... As i'm not using any ID's in my application... Just Take my source, implement and provide me the code for update... i would be grateful to u:-))
Hi, I tried this...no errors but while updating the whole row is getting null values... maybe problem with textboxes!! string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
rookie tiro
Member
86 Points
130 Posts
How to update GridView?
Apr 21, 2012 07:17 PM|LINK
Here i'm using a table to insert data.....as shown below and Below that is a Grid view with SQLdatasource.. I want to insert data only into the table and i want to edit and update the data in grid view....plz guide me.. <table align="center" cellpadding="2" cellspacing="1" style="background-color: #FF0066"> <tr> <td> NAME:</td> <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> </tr> <tr> <td> DESIGNATION:</td> <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> </tr> <tr> <td> SALARY:</td> <td> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </td> </tr> <tr> <td> CITY:</td> <td> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> </td> </tr> <tr> <td> E-MAIL:</td> <td> <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> </td> </tr> <tr align="center"> <td colspan="2"> <asp:Button ID="Button1" runat="server" BackColor="#3333FF" BorderColor="#000099" ForeColor="White" onclick="Button1_Click" Text="SUBMIT" /> </td> </tr> </table> <asp:GridView ID="GridView1" runat="server" align="center" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" AutoGenerateColumns="false" onpageindexchanging="GridView1_PageIndexChanging" onrowcancelingedit="GridView1_RowCancelingEdit" onrowediting="GridView1_RowEditing" DataKeyNames="Name" onrowupdating="GridView1_RowUpdating"> <FooterStyle BackColor="#CCCCCC" /> <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="#CCCCCC" /> <Columns> <asp:TemplateField> <EditItemTemplate> <asp:Button ID="LinkButton1" runat="server" Text="Update" CommandName="Update"></asp:Button> <asp:Button ID="LinkButton2" runat="server" Text="Cancel" CommandName="Cancel"></asp:Button> </EditItemTemplate> <ItemTemplate> <asp:Button ID="LinkButton4" runat="server" Text="Select" CommandName="Select"></asp:Button> <asp:Button ID="LinkButton3" runat="server" Text="Edit" CommandName="Edit"></asp:Button> <asp:Button ID="LinkButton5" runat="server" Text="Delete" CommandName="Delete"></asp:Button> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <EditItemTemplate> <asp:TextBox ID="TextBox7" runat="server" Text='<%#Eval("name") %>'> </asp:TextBox> </EditItemTemplate> <ItemTemplate> <%#Eval("name") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Designation"> <EditItemTemplate> <asp:TextBox ID="TextBox8" runat="server" Text='<%#Eval("desg") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <%#Eval("desg") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Salary"> <EditItemTemplate> <asp:TextBox ID="TextBox9" runat="server" Text='<%#Eval("sal") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <%#Eval("sal") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="City"> <EditItemTemplate> <asp:TextBox ID="TextBox10" runat="server" Text='<%#Eval("city") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <%#Eval("city") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email"> <EditItemTemplate> <asp:TextBox ID="TextBox11" runat="server" Text='<%#Eval("email") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <%#Eval("email") %> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>tusharrs
Contributor
3230 Points
668 Posts
Re: How to update GridView?
Apr 21, 2012 07:35 PM|LINK
Hi,
refer to this link
http://www.asp.net/web-forms/tutorials/data-access/accessing-the-database-directly-from-an-aspnet-page/inserting-updating-and-deleting-data-with-the-sqldatasource-vb
http://www.asp.net/web-forms/tutorials/data-access/accessing-the-database-directly-from-an-aspnet-page/querying-data-with-the-sqldatasource-control-cs
( Mark as Answer if it helps you out )
View my Blog
rookie tiro
Member
86 Points
130 Posts
Re: How to update GridView?
Apr 21, 2012 07:46 PM|LINK
Need it this way... public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindgrid(); } } public void bindgrid() { SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True"); SqlCommand cmd = new SqlCommand("Select * from emp", con); con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); cmd.ExecuteScalar(); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=STD-258E51EA446\\SQLEXPRESS;Initial Catalog=Employee;Integrated Security=True"); SqlCommand cmd = new SqlCommand("insert into emp(name,desg,sal,city,email) values('" + TextBox1.Text + "','" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + TextBox4.Text + "','" + TextBox5.Text + "')", con); con.Open(); cmd.ExecuteNonQuery(); bindgrid(); con.Close(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bindgrid(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bindgrid(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; bindgrid(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to update GridView?
Apr 23, 2012 02:24 AM|LINK
Hello rookie tiro:)
Just want to confirm that whether your problem get solved?Any question or feedback,Welcome your info……
PS:Yes,you can handle gridview's events by re-writing them,and I'd like to show this example done by me about "How to do batch update with the help of GridView in the form of Excel-Like thing……",You can download it freely to see the original codes……
http://code.msdn.microsoft.com/CSASPNETExcelLikeGridView-a88756b3
rookie tiro
Member
86 Points
130 Posts
Re: How to update GridView?
Apr 23, 2012 04:04 AM|LINK
tusharrs
Contributor
3230 Points
668 Posts
Re: How to update GridView?
Apr 23, 2012 04:17 AM|LINK
Hi,
You should take a primark key in your emp table, as it will be helpful in lot of scenarios.
Take one more templatefield
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:TextBox ID="txtid" runat="server" Text='<%#Eval("id") %>' style='display:none'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
textbox will hide using style=display:none
in gridview updating event retrieve the id this way,
TextBox txtid= (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtid");String id ="";
id = txtid.Text.ToString();
get all the values of other fields in this way
and then write your update query to update the record using where condition with id
( Mark as Answer if it helps you out )
View my Blog
rookie tiro
Member
86 Points
130 Posts
Re: How to update GridView?
Apr 23, 2012 05:15 AM|LINK
amit.jain
Star
11225 Points
1815 Posts
Re: How to update GridView?
Apr 23, 2012 05:19 AM|LINK
refer http://csharpdotnetfreak.blogspot.com/2009/05/gridview-sqldatasource-insert-edit.html
amiT jaiN
ASP.NET C# VB Articles And Code Examples
tusharrs
Contributor
3230 Points
668 Posts
Re: How to update GridView?
Apr 23, 2012 05:24 AM|LINK
Hello,
instead of this line GridView1.DataKeys[e.RowIndex].Value.ToString();
use this
int id= Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[datakeyname].ToString());
Here datakeyname is the data key nam which you have set to the gridivew
If datakey not working you can also take the id in a textbox as I said in previous post
retrieve that id from textbox and use the value
( Mark as Answer if it helps you out )
View my Blog
rookie tiro
Member
86 Points
130 Posts
Re: How to update GridView?
Apr 23, 2012 05:51 AM|LINK