datagrid的内容的增删改 很简单的问题呀?你设置DataGrid的EditIndex为你想要编辑的行号就行了,但是真正的Update\Delete\Insert等对数据的实际操作是要自己写代码的,并不是你点击了编辑,系统就会自动把数据给你更新到数据库中的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你说的功能,datagrid里本身就自带的,别人谈的编辑问题可是高难度的。 我也是个新手,我这有代码,你看一下吧,希望能帮得上你的忙。有些功能还没实现,你帮我看一下,互相学习,好吗?SqlConnection myConnection; protected void Page_Load(Object Src, EventArgs E) { myConnection = new SqlConnection("server=10.88.17.191;database=cg;Uid=hr;Pwd=bgphr;"); if (!IsPostBack) BindGrid(); } protected void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e) { BindGrid(e.SortExpression); } public void BindGrid(String sortfield) { SqlDataAdapter myCommand = new SqlDataAdapter("select * from authors", myConnection); DataSet ds = new DataSet(); myCommand.Fill(ds, "Authors"); DataView Source = ds.Tables["Authors"].DefaultView; Source.Sort = sortfield; MyDataGrid.DataSource=Source; MyDataGrid.DataBind(); } public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e) { String deleteCmd = "DELETE from authors where au_id = @Id"; SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection); myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11)); myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex]; myCommand.Connection.Open(); try { myCommand.ExecuteNonQuery(); Message.InnerHtml = "<b>Record Deleted</b><br>" + deleteCmd; } catch (SqlException) { Message.InnerHtml = "ERROR: Could not delete record"; Message.Style["color"] = "red"; } myCommand.Connection.Close(); BindGrid(); } public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e) { MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex; BindGrid(); for (int i = 0; i < e.Item.Cells.Count; i++) { e.Item.Cells[i].Text = Server.HtmlDecode(e.Item.Cells[i].Text); Trace.Write(e.Item.Cells[i].Text); } } public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e) { MyDataGrid.EditItemIndex = -1; BindGrid(); } public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e) { String updateCmd = "UPDATE authors SET au_id = @Id, au_lname = @LName, au_fname = @FName, phone = @Phone, " + "address = @Address, city = @City, 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("@Zip", SqlDbType.NChar, 5)); myCommand.Parameters.Add(new SqlParameter("@Contract", SqlDbType.NVarChar, 1)); myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex]; String[] cols = {"@Id","@LName","@FName","@Phone","@Address","@City","@Zip","@Contract"}; int numCols = e.Item.Cells.Count; for (int i=2; i<numCols-1; i++) //skip first, second and last column { String colvalue =((TextBox)e.Item.Cells[i].Controls[0]).Text; // check for null values in required fields if (i<6 && colvalue == "") { Message.InnerHtml = "ERROR: Null values not allowed for Author ID, Name or Phone"; Message.Style["color"] = "red"; return; } myCommand.Parameters[cols[i-1]].Value = Server.HtmlEncode(colvalue); } //append last row, converting true/false values to 0/1 if (String.Compare(((TextBox)e.Item.Cells[numCols-1].Controls[0]).Text, "true", true)==0) myCommand.Parameters["@Contract"].Value = "1"; else myCommand.Parameters["@Contract"].Value = "0"; myCommand.Connection.Open(); try { myCommand.ExecuteNonQuery(); Message.InnerHtml = "<b>Record Updated</b><br>" + updateCmd; MyDataGrid.EditItemIndex = -1; } catch (SqlException exc) { if (exc.Number == 2627) Message.InnerHtml = "ERROR: A record already exists with the same primary key"; else Message.InnerHtml = "ERROR: Could not update record, please ensure the fields are correctly filled out"; Message.Style["color"] = "red"; } myCommand.Connection.Close(); BindGrid(); } private void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if (e.Item.ItemType == ListItemType.EditItem) { for (int i = 0; i < e.Item.Controls.Count; i++) { if (e.Item.Controls[i].Controls[0].GetType().ToString() == "System.Web.UI.WebControls.TextBox") { TextBox tb = (TextBox)e.Item.Controls[i].Controls[0]; tb.Text = Server.HtmlDecode(tb.Text); } } } } public void BindGrid() { SqlDataAdapter myCommand = new SqlDataAdapter("select * from authors", myConnection); DataSet ds = new DataSet(); myCommand.Fill(ds, "Authors"); MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView; MyDataGrid.DataBind(); }<form id="Form1" method="post" runat="server"> <FONT face="宋体"><span id="Message" runat="server"></span> <asp:DataGrid id="MyDataGrid" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 56px" runat="server" Height="267px" Width="696px" CellPadding="3" DataKeyField="au_id" BorderColor="Black" BorderStyle="Outset" BackColor="PaleTurquoise" BorderWidth="2px" AllowPaging="True" PageSize="5" AutoGenerateColumns="False" AllowSorting="True"> <EditItemStyle Wrap="False"></EditItemStyle> <Columns> <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="修改"> <ItemStyle Wrap="False"></ItemStyle> </asp:EditCommandColumn> <asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn> <asp:BoundColumn DataField="au_id" SortExpression="au_id" HeaderText="au_id"> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="au_lname" SortExpression="au_lname" HeaderText="au_lname"></asp:BoundColumn> <asp:BoundColumn DataField="au_fname" SortExpression="au_fname" HeaderText="au_fname"></asp:BoundColumn> <asp:BoundColumn DataField="phone" SortExpression="phone" HeaderText="phone"></asp:BoundColumn> <asp:BoundColumn DataField="address" SortExpression="address" HeaderText="address"></asp:BoundColumn> <asp:BoundColumn DataField="zip" SortExpression="zip" HeaderText="zip"></asp:BoundColumn> <asp:BoundColumn DataField="contract" SortExpression="contract" HeaderText="contract"></asp:BoundColumn> <asp:HyperLinkColumn DataNavigateUrlField="au_id" DataNavigateUrlFormatString="datagrid_details.aspx?id={0}" Text="详细情况"></asp:HyperLinkColumn> </Columns> <PagerStyle NextPageText="下一页" PrevPageText="上一页" HorizontalAlign="Right"></PagerStyle> </asp:DataGrid></FONT> </form> C# 如何把命令行窗口执行的结果同步显示到前台界面上 把gridview控件保存到数据库 谁有我这种需求的主页面? 求一个正则 一个关于字符串的小问题!在线等 弹出窗口中点击确定后自动关闭! asp.net中请求的问题 大家帮我看看这个问题如果解决!! 在ASP.NET MVC3中,<%与MVC4中的@ 怎样将带有"<"">"符号的内容写入XML文件中阿 C#怎样写一个传递二维数组的方法呢? 求助!!!!!
有些功能还没实现,你帮我看一下,互相学习,好吗?
SqlConnection myConnection; protected void Page_Load(Object Src, EventArgs E)
{
myConnection = new SqlConnection("server=10.88.17.191;database=cg;Uid=hr;Pwd=bgphr;"); if (!IsPostBack)
BindGrid();
}
protected void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
BindGrid(e.SortExpression);
}
public void BindGrid(String sortfield)
{
SqlDataAdapter myCommand = new SqlDataAdapter("select * from authors", myConnection); DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors"); DataView Source = ds.Tables["Authors"].DefaultView;
Source.Sort = sortfield; MyDataGrid.DataSource=Source;
MyDataGrid.DataBind();
} public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e)
{
String deleteCmd = "DELETE from authors where au_id = @Id"; SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex]; myCommand.Connection.Open(); try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>Record Deleted</b><br>" + deleteCmd;
}
catch (SqlException)
{
Message.InnerHtml = "ERROR: Could not delete record";
Message.Style["color"] = "red";
} myCommand.Connection.Close(); BindGrid();
}
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex; BindGrid();
for (int i = 0; i < e.Item.Cells.Count; i++)
{
e.Item.Cells[i].Text = Server.HtmlDecode(e.Item.Cells[i].Text);
Trace.Write(e.Item.Cells[i].Text);
}
} public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
} public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
{
String updateCmd = "UPDATE authors SET au_id = @Id, au_lname = @LName, au_fname = @FName, phone = @Phone, "
+ "address = @Address, city = @City, 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("@Zip", SqlDbType.NChar, 5));
myCommand.Parameters.Add(new SqlParameter("@Contract", SqlDbType.NVarChar, 1)); myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex]; String[] cols = {"@Id","@LName","@FName","@Phone","@Address","@City","@Zip","@Contract"}; int numCols = e.Item.Cells.Count;
for (int i=2; i<numCols-1; i++) //skip first, second and last column
{
String colvalue =((TextBox)e.Item.Cells[i].Controls[0]).Text; // check for null values in required fields
if (i<6 && colvalue == "")
{
Message.InnerHtml = "ERROR: Null values not allowed for Author ID, Name or Phone";
Message.Style["color"] = "red";
return;
} myCommand.Parameters[cols[i-1]].Value = Server.HtmlEncode(colvalue);
} //append last row, converting true/false values to 0/1
if (String.Compare(((TextBox)e.Item.Cells[numCols-1].Controls[0]).Text, "true", true)==0)
myCommand.Parameters["@Contract"].Value = "1";
else
myCommand.Parameters["@Contract"].Value = "0"; myCommand.Connection.Open(); try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>Record Updated</b><br>" + updateCmd;
MyDataGrid.EditItemIndex = -1;
}
catch (SqlException exc)
{
if (exc.Number == 2627)
Message.InnerHtml = "ERROR: A record already exists with the same primary key";
else
Message.InnerHtml = "ERROR: Could not update record, please ensure the fields are correctly filled out";
Message.Style["color"] = "red";
} myCommand.Connection.Close(); BindGrid();
} private void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
for (int i = 0; i < e.Item.Controls.Count; i++)
{
if (e.Item.Controls[i].Controls[0].GetType().ToString() == "System.Web.UI.WebControls.TextBox")
{
TextBox tb = (TextBox)e.Item.Controls[i].Controls[0];
tb.Text = Server.HtmlDecode(tb.Text);
}
}
}
}
public void BindGrid()
{
SqlDataAdapter myCommand = new SqlDataAdapter("select * from authors", myConnection); DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors"); MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
MyDataGrid.DataBind();
}
<form id="Form1" method="post" runat="server">
<FONT face="宋体"><span id="Message" runat="server"></span>
<asp:DataGrid id="MyDataGrid" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 56px" runat="server" Height="267px" Width="696px" CellPadding="3" DataKeyField="au_id" BorderColor="Black" BorderStyle="Outset" BackColor="PaleTurquoise" BorderWidth="2px" AllowPaging="True" PageSize="5" AutoGenerateColumns="False" AllowSorting="True">
<EditItemStyle Wrap="False"></EditItemStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="修改">
<ItemStyle Wrap="False"></ItemStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="au_id" SortExpression="au_id" HeaderText="au_id">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="au_lname" SortExpression="au_lname" HeaderText="au_lname"></asp:BoundColumn>
<asp:BoundColumn DataField="au_fname" SortExpression="au_fname" HeaderText="au_fname"></asp:BoundColumn>
<asp:BoundColumn DataField="phone" SortExpression="phone" HeaderText="phone"></asp:BoundColumn>
<asp:BoundColumn DataField="address" SortExpression="address" HeaderText="address"></asp:BoundColumn>
<asp:BoundColumn DataField="zip" SortExpression="zip" HeaderText="zip"></asp:BoundColumn>
<asp:BoundColumn DataField="contract" SortExpression="contract" HeaderText="contract"></asp:BoundColumn>
<asp:HyperLinkColumn DataNavigateUrlField="au_id" DataNavigateUrlFormatString="datagrid_details.aspx?id={0}" Text="详细情况"></asp:HyperLinkColumn>
</Columns>
<PagerStyle NextPageText="下一页" PrevPageText="上一页" HorizontalAlign="Right"></PagerStyle>
</asp:DataGrid></FONT>
</form>