Inline Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="400px" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField ReadOnly="true" DataField="UserID"
HeaderText="UserId" SortExpression="UserID" />
<asp:BoundField DataField="Email" HeaderText="Category_ID"
InsertVisible="False" ReadOnly="false" SortExpression="Email" />
<asp:BoundField DataField="BPName" HeaderText="BPName"
SortExpression="BPName" />
<asp:CommandField ButtonType="Link" ShowEditButton="true" />
</Columns>
</asp:GridView>
Code in Code Behind:
using System;
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;
public partial class _Default : System.Web.UI.Page
{
int i;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
private void BindGrid()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
con.Open();
if (con.State == ConnectionState.Open)
{
Response.Write("Connection is Open");
}
else
{
Response.Write("Connection is Close");
}
SqlCommand cmd = new SqlCommand("Ds_Select_CountryBP");
DataSet ds = new DataSet();
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText.ToString(), con);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = - 1;
//BindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
i = e.RowIndex;
UpdateGridData();
//string i1 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
//Response.Write(i1);
//GridView drow = GridView1.SelectedRow;
//Int32 i1 =drow.ce
//string s = GridView1.Rows[i1].Cells[1].Text.ToString();
//Response.Write(s);
//GridView1.EditIndex = -1;
//SqlConnection con = new SqlConnection();
//con.ConnectionString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
//con.Open();
//if (con.State == ConnectionState.Open)
//{
// Response.Write("Connection is Open");
//}
//else
//{
// Response.Write("Connection is Close");
//}
//i = e.RowIndex;
//string Email = GridView1.Rows[i].Cells[1].Text.ToString();
//int UserId = Convert.ToInt32(GridView1.Rows[i].Cells[0].Controls[0].ToString());
//string BPName = GridView1.Rows[i].Cells[2].Text.ToString();
//SqlCommand cmd = new SqlCommand("update Test1 set Email=@Email, BPName=@BPName,Userid=@UserId");
//cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 50, Email);
//cmd.Parameters.Add("@BPName", SqlDbType.NVarChar, 50, BPName);
//cmd.Parameters.Add("@UserId", SqlDbType.Int, 4, UserId.ToString());
//DataSet ds = new DataSet();
//cmd.CommandType = CommandType.Text;
//SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText.ToString(), con);
//da.Fill(ds);
//GridView1.DataSource = ds;
//GridView1.DataBind();
}
private void UpdateGridData()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
con.Open();
if (con.State == ConnectionState.Open)
{
Response.Write("Connection is Open");
}
else
{
Response.Write("Connection is Close");
}
GridViewRow drow = GridView1.SelectedRow;
//string i1 = drow.Cells[2].Text;
//string s = GridView1.Rows[i1].Cells[1].Text.ToString();
//Response.Write(s);
//GridView1.EditIndex = -1;
// i = 2;
//i = GridView1.SelectedRow.RowIndex;
string Email = ((TextBox)(GridView1.Rows[i].Cells[1].Controls[0])).Text.ToString();
int UserId = Convert.ToInt32(GridView1.Rows[i].Cells[0].Text);
string BPName = ((TextBox)(GridView1.Rows[i].Cells[2].Controls[0])).Text.ToString();
SqlCommand cmd = new SqlCommand("update Test1 set Email=@Email, BPName=@BPName where userid=@userid",con);
cmd.Parameters.Add("@Email",SqlDbType.NVarChar,50);
cmd.Parameters.Add("@BPName", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("@UserId", SqlDbType.Int);
cmd.Parameters[0].Value = Email;
cmd.Parameters[1].Value = BPName;
cmd.Parameters[2].Value = UserId;
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
BindGrid();
//DataSet ds = new DataSet();
//cmd.CommandType = CommandType.Text;
//SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText.ToString(), con);
//da.Fill(ds);
//GridView1.DataSource = ds;
//GridView1.DataBind();
}
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="400px" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField ReadOnly="true" DataField="UserID"
HeaderText="UserId" SortExpression="UserID" />
<asp:BoundField DataField="Email" HeaderText="Category_ID"
InsertVisible="False" ReadOnly="false" SortExpression="Email" />
<asp:BoundField DataField="BPName" HeaderText="BPName"
SortExpression="BPName" />
<asp:CommandField ButtonType="Link" ShowEditButton="true" />
</Columns>
</asp:GridView>
Code in Code Behind:
using System;
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;
public partial class _Default : System.Web.UI.Page
{
int i;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
private void BindGrid()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
con.Open();
if (con.State == ConnectionState.Open)
{
Response.Write("Connection is Open");
}
else
{
Response.Write("Connection is Close");
}
SqlCommand cmd = new SqlCommand("Ds_Select_CountryBP");
DataSet ds = new DataSet();
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText.ToString(), con);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = - 1;
//BindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
i = e.RowIndex;
UpdateGridData();
//string i1 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
//Response.Write(i1);
//GridView drow = GridView1.SelectedRow;
//Int32 i1 =drow.ce
//string s = GridView1.Rows[i1].Cells[1].Text.ToString();
//Response.Write(s);
//GridView1.EditIndex = -1;
//SqlConnection con = new SqlConnection();
//con.ConnectionString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
//con.Open();
//if (con.State == ConnectionState.Open)
//{
// Response.Write("Connection is Open");
//}
//else
//{
// Response.Write("Connection is Close");
//}
//i = e.RowIndex;
//string Email = GridView1.Rows[i].Cells[1].Text.ToString();
//int UserId = Convert.ToInt32(GridView1.Rows[i].Cells[0].Controls[0].ToString());
//string BPName = GridView1.Rows[i].Cells[2].Text.ToString();
//SqlCommand cmd = new SqlCommand("update Test1 set Email=@Email, BPName=@BPName,Userid=@UserId");
//cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 50, Email);
//cmd.Parameters.Add("@BPName", SqlDbType.NVarChar, 50, BPName);
//cmd.Parameters.Add("@UserId", SqlDbType.Int, 4, UserId.ToString());
//DataSet ds = new DataSet();
//cmd.CommandType = CommandType.Text;
//SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText.ToString(), con);
//da.Fill(ds);
//GridView1.DataSource = ds;
//GridView1.DataBind();
}
private void UpdateGridData()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
con.Open();
if (con.State == ConnectionState.Open)
{
Response.Write("Connection is Open");
}
else
{
Response.Write("Connection is Close");
}
GridViewRow drow = GridView1.SelectedRow;
//string i1 = drow.Cells[2].Text;
//string s = GridView1.Rows[i1].Cells[1].Text.ToString();
//Response.Write(s);
//GridView1.EditIndex = -1;
// i = 2;
//i = GridView1.SelectedRow.RowIndex;
string Email = ((TextBox)(GridView1.Rows[i].Cells[1].Controls[0])).Text.ToString();
int UserId = Convert.ToInt32(GridView1.Rows[i].Cells[0].Text);
string BPName = ((TextBox)(GridView1.Rows[i].Cells[2].Controls[0])).Text.ToString();
SqlCommand cmd = new SqlCommand("update Test1 set Email=@Email, BPName=@BPName where userid=@userid",con);
cmd.Parameters.Add("@Email",SqlDbType.NVarChar,50);
cmd.Parameters.Add("@BPName", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("@UserId", SqlDbType.Int);
cmd.Parameters[0].Value = Email;
cmd.Parameters[1].Value = BPName;
cmd.Parameters[2].Value = UserId;
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
BindGrid();
//DataSet ds = new DataSet();
//cmd.CommandType = CommandType.Text;
//SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText.ToString(), con);
//da.Fill(ds);
//GridView1.DataSource = ds;
//GridView1.DataBind();
}
}
No comments:
Post a Comment