The Way to Programming
The Way to Programming
HTML
C# Code
using System; using System.Web.UI.WebControls; using System.Data; using System.Linq; using System.Configuration; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.BindGrid(); } } private void BindGrid() { SqlCommand cmd = new SqlCommand("SELECT top 10 CustomerId, ContactName, Country FROM Customers"); gvCustomers.DataSource = this.ExecuteQuery(cmd, "SELECT"); gvCustomers.DataBind(); } private DataTable ExecuteQuery(SqlCommand cmd, string action) { string conString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString; using (SqlConnection con = new SqlConnection(conString)) { cmd.Connection = con; switch (action) { case "SELECT": using (SqlDataAdapter sda = new SqlDataAdapter()) { sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); return dt; } } case "UPDATE": con.Open(); cmd.ExecuteNonQuery(); con.Close(); break; } return null; } } protected void Update(object sender, EventArgs e) { foreach (GridViewRow row in gvCustomers.Rows) { if (row.RowType == DataControlRowType.DataRow) { bool isChecked = row.Cells[0].Controls.OfType().FirstOrDefault().Checked; if (isChecked) { SqlCommand cmd = new SqlCommand("UPDATE Customers SET ContactName = @ContactName, Country = @Country WHERE CustomerId = @CustomerId"); cmd.Parameters.AddWithValue("@ContactName", row.Cells[1].Controls.OfType ().FirstOrDefault().Text); cmd.Parameters.AddWithValue("@Country", row.Cells[2].Controls.OfType ().FirstOrDefault().SelectedItem.Value); cmd.Parameters.AddWithValue("@CustomerId", gvCustomers.DataKeys[row.RowIndex].Value); this.ExecuteQuery(cmd, "SELECT"); } } } btnUpdate.Visible = false; this.BindGrid(); } protected void OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { SqlCommand cmd = new SqlCommand("SELECT DISTINCT(Country) FROM Customers"); DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList); ddlCountries.DataSource = this.ExecuteQuery(cmd, "SELECT"); ddlCountries.DataTextField = "Country"; ddlCountries.DataValueField = "Country"; ddlCountries.DataBind(); string country = (e.Row.FindControl("lblCountry") as Label).Text; ddlCountries.Items.FindByValue(country).Selected = true; } } protected void OnCheckedChanged(object sender, EventArgs e) { bool isUpdateVisible = false; CheckBox chk = (sender as CheckBox); if (chk.ID == "chkAll") { foreach (GridViewRow row in gvCustomers.Rows) { if (row.RowType == DataControlRowType.DataRow) { row.Cells[0].Controls.OfType ().FirstOrDefault().Checked = chk.Checked; } } } CheckBox chkAll = (gvCustomers.HeaderRow.FindControl("chkAll") as CheckBox); chkAll.Checked = true; foreach (GridViewRow row in gvCustomers.Rows) { if (row.RowType == DataControlRowType.DataRow) { bool isChecked = row.Cells[0].Controls.OfType ().FirstOrDefault().Checked; for (int i = 1; i < row.Cells.Count; i++) { row.Cells[i].Controls.OfType
Sign in to your account