
Prepare GridView
1.GridView -> Show Smart Tag -> Edit Columns -> Uncheck Auto-Generate Field
2.Add three TemplateField Column for Employee Name, Designation, & Delete Button.
3.Add Edit button from CommandField Group from Property Window.
4.Each template columns Item Template field contains Label Control
5.Edit Template contains TextBox control for Editing item. Set the Binding Field name to the Text Property of both controls
for each template field to the respective Database Column Name i.e Eval("EmpName").
6.Set the DataKeyNames property of GridView to Primary Key Column of DataBase i.e. EmpId.
7.Set the data bindings for Delete Button for CommandArgument Eval("EmpId");
8.Set the CommandName property to Delete Button to CMDDelete.
9.Place the Textbox control in the grids Footer template for Adding new record
10. Set the CommandName to CMDAdd for Add button
11.Register events for Edit, Update, Cancel button of gridview RowEditing, RowUpdating, RowCancelEditing=======================
Fill &View
1.Call the BindGrid on Page_Load in !IsPostBack block to fill the grid by default.
pprivate void BindGrid()
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
// -----------------------------------
pprivate DataSet GetData()
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["neoftpConnectionString"].ConnectionString;
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("Select * From Languages", conn);
try
{
da.Fill(ds);
return ds;
}
catch { }
finally
{
conn.Close();
conn.Dispose();
}
return null;
}
=======================
Edit Gridview
Register RowEditing event of GridView
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();
}
=======================
Update in GridView
1.register RowUpdating event of the gridview.
2.Find the Unique id for updating the row from DataKeys collection of gridview
int IdLang= Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
3.Find the controls in the selected row
TextBox TextLanguage = GridView1.Rows[e.RowIndex].FindControl("LanguageTextBox") as TextBox;
TextBox TextDescription = GridView1.Rows[e.RowIndex].FindControl("DescTextBox") as TextBox;
4.Finally update the row and refresh the grid.
if (TextLanguage != null && TextDescription != null)
{
SetSqlCommand("UPDATE [Languages]SET [Language] ='" + TextLanguage.Text.Trim() + "',[Description] = '" + TextDescription.Text.Trim() + "' WHERE IdLang=" + IdLang);
GridView1.EditIndex = -1;
BindGrid();
}
--------------------- complete ----------------------
using System.Data.SqlClient;
DataSet ds = new DataSet();
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
SqlCommand myCommand;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// GetData();
BindGrid();
}
}
private void BindGrid()
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
private DataSet GetData()
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["neoftpConnectionString"].ConnectionString;
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("Select * From Languages", conn);
try
{
da.Fill(ds);
return ds;
}
catch { }
finally
{
conn.Close();
conn.Dispose();
}
return null;
}
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_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int IdLang= Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
TextBox TextLanguage = GridView1.Rows[e.RowIndex].FindControl("LanguageTextBox") as TextBox;
TextBox TextDescription = GridView1.Rows[e.RowIndex].FindControl("DescTextBox") as TextBox;
if (TextLanguage != null && TextDescription != null)
{
//UpdateLanguage(IdLang, TextLanguage.Text.Trim(), TextDescription.Text.Trim());
SetSqlCommand("UPDATE [Languages]SET [Language] ='" + TextLanguage.Text.Trim() + "',[Description] = '" + TextDescription.Text.Trim() + "' WHERE IdLang=" + IdLang);
GridView1.EditIndex = -1;
BindGrid();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("CMDDelete"))
{
int IdLang = Convert.ToInt32(e.CommandArgument);
//DeleteLanguage(IdLang);
SetSqlCommand("DELETE FROM [Languages] WHERE IdLang=" + IdLang);
BindGrid();
}
else if (e.CommandName.Equals("CMDAdd"))
{
TextBox TextLanguage = GridView1.FooterRow.FindControl("LanguageTextBox") as TextBox;
TextBox TextDescription = GridView1.FooterRow.FindControl("DescTextBox") as TextBox;
if (TextLanguage != null && TextDescription != null)
{
//AddLanguage(TextLanguage.Text.Trim(), TextDescription.Text.Trim());
SetSqlCommand("INSERT INTO [Languages]([Language],[Description]) VALUES('" + TextLanguage.Text.Trim() + "','" + TextDescription.Text.Trim() + "')");
GridView1.EditIndex = -1;
BindGrid();
}
}
}
private void SetSqlCommand(string mySqlCommand)
{
try
{
//string myInsertQuery = "INSERT INTO [Languages]([Language],[Description]) VALUES('" + Language + "','" + Description + "')";
conn.ConnectionString = ConfigurationManager.ConnectionStrings["neoftpConnectionString"].ConnectionString;
conn.Open();
myCommand = new SqlCommand(mySqlCommand, conn);
myCommand.CommandText = mySqlCommand;
myCommand.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e);
}
finally
{
conn.Close();
conn.Dispose();
}
}