Datatable 虚拟表操作
时间:2011-05-21 来源:MzXy
Datagird表格
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" BorderColor="#ADAAAD" ShowFooter="True" Width="255px" oncancelcommand="DataGrid1_CancelCommand" ondeletecommand="DataGrid1_DeleteCommand" oneditcommand="DataGrid1_EditCommand" onupdatecommand="DataGrid1_UpdateCommand"> <AlternatingItemStyle BackColor="#EFEFEF"></AlternatingItemStyle> <ItemStyle BackColor="White"></ItemStyle> <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#00659C"></HeaderStyle> <Columns> <asp:BoundColumn DataField="recordid" HeaderText="单据编号"> <HeaderStyle Wrap="False"></HeaderStyle> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="depotcode" HeaderText="仓库编码"> <HeaderStyle Wrap="False" ForeColor="White"></HeaderStyle> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="batchno" HeaderText="样品批号"> <HeaderStyle Wrap="False" ForeColor="White"></HeaderStyle> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="SampleID" HeaderText="样品编码"> <HeaderStyle Wrap="False" ForeColor="White"></HeaderStyle> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> <asp:BoundColumn DataField="remain" HeaderText="数量"> <HeaderStyle Wrap="False" ForeColor="White"></HeaderStyle> <ItemStyle Wrap="False"></ItemStyle> </asp:BoundColumn> <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"> <HeaderStyle Wrap="False"></HeaderStyle> </asp:EditCommandColumn> <asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn> </Columns> <PagerStyle Mode="NumericPages"></PagerStyle> </asp:datagrid>
后台执行语句
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { ViewState["dt"] = GetDataTable(); } } /// <summary> /// 创建临时表 /// </summary> /// <returns></returns> public DataTable GetDataTable() { //建表 DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn( "recordid", typeof(string))); dt.Columns.Add(new DataColumn( "depotcode", typeof(string))); dt.Columns.Add(new DataColumn( "batchno", typeof(string))); DataColumn SampColumn= dt.Columns.Add( "SampleID", typeof(string)); dt.Columns.Add(new DataColumn( "remain", typeof(decimal))); dt.PrimaryKey = new DataColumn[] { SampColumn }; //设置主键 return dt; } /// <summary> /// 取消 /// </summary> /// <param name="source"></param> /// <param name="e"></param> protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e) { DataGrid1.EditItemIndex = -1; DataGrid1.DataSource = (DataTable)ViewState["dt"]; DataGrid1.DataBind(); } /// <summary> /// 删除 /// </summary> /// <param name="source"></param> /// <param name="e"></param> protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e) { DataTable dt=(DataTable)ViewState["dt"]; DataRow dr = dt.Rows.Find(e.Item.Cells[3].Text); dt.Rows.Remove(dr); ViewState["dt"] = dt; DataGrid1.DataSource = (DataTable)ViewState["dt"]; DataGrid1.DataBind(); } /// <summary> /// 编辑 /// </summary> /// <param name="source"></param> /// <param name="e"></param> protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e) { ViewState["Sample"] = e.Item.Cells[3].Text; DataGrid1.EditItemIndex = e.Item.ItemIndex; DataGrid1.DataSource = (DataTable)ViewState["dt"]; DataGrid1.DataBind(); } /// <summary> /// 更新 /// </summary> /// <param name="source"></param> /// <param name="e"></param> protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e) { DataTable dt = (DataTable)ViewState["dt"]; DataRow dr = dt.Rows.Find(ViewState["Sample"].ToString()); dr[0] =((TextBox)e.Item.Cells[0].Controls[0]).Text; dr[1] = ((TextBox)e.Item.Cells[1].Controls[0]).Text; dr[2] = ((TextBox)e.Item.Cells[2].Controls[0]).Text; dr[3] = ((TextBox)e.Item.Cells[3].Controls[0]).Text; dr[4] = ((TextBox)e.Item.Cells[4].Controls[0]).Text; ViewState["dt"] = dt; DataGrid1.EditItemIndex = -1; DataGrid1.DataSource = (DataTable)ViewState["dt"]; DataGrid1.DataBind(); }
相关阅读 更多 +