数据成功显示呢,请问怎样给才能实现编辑/删除功能呢?因为是dataTable绑定的数据,所以有一些简单打方法用不了。本人小白求赐教。。。
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\equipment.mdf;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
//多表查询获取数据
{
if (!IsPostBack)
{
string SQL1 = "SELECT emp.emp_id AS 员工编号, emp.emp_name AS 员工姓名, emp.emp_phone AS 联系电话, dept.dept_name AS 所属部门 FROM emp,dept where emp.dept_id =dept.dept_id";
DataTable dt1 = ExecuteSqlGetDataTable(SQL1);
GridView1.DataSource = dt1;
GridView1.DataBind();
}
}
public DataTable ExecuteSqlGetDataTable(string safeSql)
{
SqlConnection sqlConn = new SqlConnection(strCon);
sqlConn.Open();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, sqlConn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
sqlConn.Close();
return ds.Tables[0];
}
//前端页面的GridView
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataPropertyName="emp"
OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:ButtonField CommandName="Delete" Text="删除" ButtonType="Link"></asp:ButtonField>
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
</Columns>
</asp:GridView>
</div>
</form>