AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 How to edit,update,delete gridview data and displaying the data using dropdownlist in gridview
Posted by Rajanikanth on  3/3/2013 3:46:53 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
Hi,

I am Asp.Net and C# coding.

I am displaying sql server table data in gridview based on the selection of dropdownlist.

now my problem is iam unable to use the events of update and cancel events. i am able to use the edit event in gridview but unable to use those update and cancel after clicking edit.

please tell me how use update, cancel and delete options for Gridview.

Thanks in advance.

html code:
----------------
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
font-weight: bold;
text-decoration: underline;
font-size: x-large;
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<h5 class="style1">
Data Grid View Sample</h5>

</div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="-- Select --" Value=""></asp:ListItem>
<asp:ListItem Text="Emp" Value="Emp"></asp:ListItem>
<asp:ListItem Text="Dept" Value="Dept"></asp:ListItem>
</asp:DropDownList>

<br />

<br />
<b>Grid View:</b>
<br />
<br />

<asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
CellSpacing="2" ForeColor="Black" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<RowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<EmptyDataTemplate>
&nbsp;
</EmptyDataTemplate>
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</form>

</body>
</html>

.aspx code:
-------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace DataGridView_Sample
{
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=SHINY-PC\\SQLEXPRESS;Initial Catalog=NRK;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
DataSet ds = new DataSet();


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
con.Open();
cmd = new SqlCommand("Select name from sys.tables order by name", con);
da = new SqlDataAdapter(cmd);
da.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "name";
DropDownList1.DataValueField = "name";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("--Select--", "--Select--"));

}

}




protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex != 0)
{
cmd = new SqlCommand("select * from " + DropDownList1.SelectedItem.Value, con);
con.Open();
da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();

}

}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
cmd = new SqlCommand("select * from " + DropDownList1.SelectedItem.Value, con);
con.Open();
da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.EditIndex = Convert.ToInt16(e.NewEditIndex);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{


}


}
}

Post Your Reply Post New Question

 Viewer's Reply
Sample code for Update, Delete, Cancel in Asp.net GridView control
Answered By Moderator1 on 3/28/2013 8:20:49 AM
Hi, please find the below codes.

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");
DropDownList cmbGender = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("cmbGender");
TextBox txtCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCity");
DropDownList cmbType = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("cmbType");

customer.Update(GridView1.DataKeys[e.RowIndex].Values[0].ToString(),txtName.Text, cmbGender.SelectedValue,txtCity.Text, cmbType.SelectedValue);
GridView1.EditIndex = -1;
FillCustomerInGrid();
}


protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
customer.Delete(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
FillCustomerInGrid();
}


protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
FillCustomerInGrid();
}
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Washington Technology
Programmers Heaven C# School Book - Free 338 Page eBook
Windows 8: 50 Questions and Answers You Need to Make a Decision (Normally $9.95)