修改,删除:
在DataGrid里加一个编辑列和一个删除列,然后在DataGrid1_EditCommand(),DataGrid1_UpdateCommand(),DataGrid1_CancelCommand(),DataGrid1_DeleteCommand()事件中添加代码就OK了!!
添加:
可以另外拖放一些textBox(或者dropDownList什么的),其内容是相应的字段,在拖放一个Button,最后在Button_Click()事件中写添加代码就行了!!
在DataGrid里加一个编辑列和一个删除列,然后在DataGrid1_EditCommand(),DataGrid1_UpdateCommand(),DataGrid1_CancelCommand(),DataGrid1_DeleteCommand()事件中添加代码就OK了!!
添加:
可以另外拖放一些textBox(或者dropDownList什么的),其内容是相应的字段,在拖放一个Button,最后在Button_Click()事件中写添加代码就行了!!
DataGrid1_UpdateCommand()中的代码怎末写?
{
String updateCmd = "UPDATE Authors SET au_id = @Id, au_lname = @LName, au_fname = @FName, phone = @Phone, "
+ "address = @Address, city = @City, state = @State, zip = @Zip, contract = @Contract where au_id = @Id"; SqlCommand myCommand = new SqlCommand(updateCmd, myConnection); myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
myCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.NVarChar, 40));
myCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.NVarChar, 20));
myCommand.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NChar, 12));
myCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.NVarChar, 40));
myCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 20));
myCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.NChar, 2));
myCommand.Parameters.Add(new SqlParameter("@Zip", SqlDbType.NChar, 5));
myCommand.Parameters.Add(new SqlParameter("@Contract", SqlDbType.NVarChar,1));myCommand.Parameters["@Id"].value=e.Item.Cells[2].Text;
myCommand.Parameters["@Name"].value=e.Item.Cells[3].Text;
...
...
myCommand.Connection.Open();try
{
myCommand.ExecuteNonQuery();
}
catch (SqlException)
{
...
}
DataGrid1.EditItemIndex = -1;
}插入,删除,参照这个!!
我以前用的是VB6和DELPHI对于这种DataGrid的使用和恒心方式不太熟悉,
能不能给我发一个完整的程序研究研究,谢谢
在。net中的帮助中搜索
演练:将 XML 数据读入数据集
这个例子比较好
参数中的e!!
我要的是WinForm的,你发表的这个e参数好像是WebForm的吧?
能不能把这块详细的程序贴出来,谢谢
{
switch(e.CommandName)
{
case "Edit":
{
((DataGrid)source).EditItemIndex = e.Item.ItemIndex;
break;
}
case "Page":
{
((DataGrid)source).CurrentPageIndex = Convert.ToInt32(e.CommandArgument)--;
break;
}
case "Update":
{
SqlCommand comm = new SqlCommand("updqte table1 set ... where idx = "+e.CommandArgument,new SqlConnection("DNS"));
comm.Connection.Open();
comm.ExecuteNonQuery();
comm.Connection.Close();
comm.Dispose();
break;
}
case "Delete":
{
SqlCommand comm = new SqlCommand("delete from table1 where idx = "+e.CommandArgument,new SqlConnection("DNS"));
comm.Connection.Open();
comm.ExecuteNonQuery();
comm.Connection.Close();
comm.Dispose();
break;
}
default:
break;
}
BindGrid((DataGrid)source);
} private void BindGrid(DataGrid grid)
{
SqlDataAdapter adp = new SqlDataAdapter("sql语句","DNS");
DataTable dt = new DataTable();
adp.Fill(dt); grid.DataSource = dt;
grid.DataBind(); adp.Dispose();
dt.Dispose();
}