![](/img/trans.png)
[英]C# Devexpress How to Pass Multiple Value from WinForm 1 to WinForm 2 ? (To perform New,Edit, View and so on)
[英]How to Perform delete and Edit in sql from grid view in C#
如何在C#中從網格視圖執行SQL表中的刪除和更新記錄。 我已經以多種方式嘗試過它,但是無論如何都沒有奏效。 請幫助我解決此問題,以便在網格視圖中添加刪除和編輯功能以編輯sql表。
protected void userlist_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
userlist.DeleteRow(e.RowIndex);
}
protected void userlist_RowEditing(object sender, GridViewEditEventArgs e)
{
userlist.EditIndex = e.NewEditIndex;
DataBind();
}
protected void userlist_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection();
try
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["PMS"].ToString();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
// SqlCommand cmd = "UPDATE UserDT SET (UserId,UserName, Password, Email, PhoneNumber )" + "VALUES ('"+ UserName.Text +"' + tbxPassword.Text + tbxPermission.Text + "');";
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Note.Text = ex.Message;
}
}
您正在綁定到DataTable。 嘗試這個:
DataTable sourceData = (DataTable)userlist.DataSource;
sourceData.Rows[e.RowIndex].Delete();
userlist.DataSource = sourceData;
userlist.DataBind();
您可以如下所示使用。 對於從gridview設計列標記插入
<Columns>
<FooterTemplate>
<asp:LinkButton ID="btnAddNew" Text="Add New" runat="server" CommandName="AddNew" ToolTip="ADD"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
protected void EmpGrid_Command(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtf = (TextBox)EmpGrid.FooterRow.FindControl("txtFooterFname");
TextBox txtl = (TextBox)EmpGrid.FooterRow.FindControl("txtfooterLname");
TextBox txtq =
(TextBox)EmpGrid.FooterRow.FindControl("txtfooterqualification");
TextBox txtd =
(TextBox)EmpGrid.FooterRow.FindControl("txtfooterdecription");
TextBox txts = (TextBox)EmpGrid.FooterRow.FindControl("txtfootersalary");
// For Inserting New Row
string insrtquery = "insert into EMPTable
(fname,lname,qualification,designation,sal) values
('" + txtf.Text + "','" + txtl.Text + "',
'" + txtq.Text + "','" + txtd.Text + "','" +
txts.Text + "')";
da = new SqlDataAdapter(insrtquery, con);
DataSet ds = new DataSet();
da.Fill(ds,"inserted");
Bindemployees();
}
}
protected void EmpGrid_Updating(object sender, GridViewUpdateEventArgs e)
{
int empid = Convert.ToInt32(EmpGrid.DataKeys[e.RowIndex].Value.ToString());
string fname = EmpGrid.DataKeys[e.RowIndex].Values["fname"].ToString();
string lname = EmpGrid.DataKeys[e.RowIndex].Values["lname"].ToString();
TextBox txtq =
(TextBox)EmpGrid.Rows[e.RowIndex].FindControl("txtEditqualification");
TextBox txtd =
(TextBox)EmpGrid.Rows[e.RowIndex].FindControl("txtEditdesignation");
TextBox txts =
(TextBox)EmpGrid.Rows[e.RowIndex].FindControl("txtEditsalary");
string updatequery = "update EMPTable set qualification='" + txtq.Text +
"',designation='" + txtd.Text + "',sal='" + txts.Text + "'
where empid='" + empid + "'";
DataSet ds = new DataSet();
da = new SqlDataAdapter(updatequery,con);
da.Fill(ds,"added");
EmpGrid.EditIndex = -1;
Bindemployees();
}
protected void EmpGrid_Deleting(object sender, GridViewDeleteEventArgs e)
{
int empid = Convert.ToInt32(EmpGrid.DataKeys[e.RowIndex].Value.ToString());
da = new
SqlDataAdapter("delete from EMPTable where empid='" + empid + "'", con);
DataSet ds = new DataSet();
da.Fill(ds,"deleted");
Bindemployees();
}
為了更好的理解,請參閱此鏈接
http://reddyinfosoft.blogspot.in/2012/07/insert-edit-update-and-delete-with-in.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.