Table里嵌套ASPXGridView
简述
有时候我们在录入数据的时候,因为录入数据字段比较少,所以可以直接在GridView上录入。
但是我们有些公用字段是在单头中固定的,GridView显示的是单身。
这样为了方便我们把GridView嵌套在Table中,录入数据时就可以取到单头上的数据。
也可以根据单头来检索同类数据,编辑数据也可在GridView上操作。
ASPXGridView
这边在GridView是选择了DevExpress控件里的ASPXGridView。
这个第三方控件是美国人做的,现在应该更新到14.几了吧。
国内也有公司在代理,但是是收费的,网上也可以下载到,再自己破解下就可以用了。
Dev在GridView(表格)和PivotGrid(报表)上提供了很大集成功能,用起来很简单方便。
但是因为控件的英文的开发起来有一定难度,但是他有一个很完善的Demo可以学习。
前台ASPX
我们在页面上话一个2行6列的Table,然后把第2行的合并单元格。第一行里放表头字段及数据,第二行里放ASPXGridView。
代码如下:
<table runat="server" border="2"> <tr> <td>订单单号:</td> <td><asp:TextBox runat="server" ID="txtTA027" AutoPostBack="true" OnTextChanged="txtTA027_TextChanged"></asp:TextBox></td> <td>工单单号:</td> <td><asp:TextBox runat="server" ID="txtTA002" AutoPostBack="true"></asp:TextBox></td> <td>品号:</td> <td><asp:TextBox runat="server" ID="txtTA006" AutoPostBack="true" OnTextChanged="txtTA006_TextChanged"></asp:TextBox></td> </tr> <tr> <td colspan="6"> <dx:ASPxGridView ID="ASPxGridView1" runat="server" KeyFieldName="TA002" Width="100%" OnRowUpdating="ASPxGridView1_RowUpdating" OnRowInserting="ASPxGridView1_RowInserting" OnInitNewRow="ASPxGridView1_InitNewRow" OnPageIndexChanged="ASPxGridView1_PageIndexChanged"> <Settings ShowGroupPanel="True" /> <SettingsEditing Mode="Inline" /> <Columns> <dx:GridViewCommandColumn VisibleIndex="0"> <EditButton Visible="True"> </EditButton> <NewButton Visible="true" /> </dx:GridViewCommandColumn> <dx:GridViewDataColumn FieldName="TA002" Caption="工单单号" VisibleIndex="1" ReadOnly="false"> <Settings AllowSort="True" /> <HeaderStyle HorizontalAlign="Center" Font-Bold ="true" ForeColor ="white"/> <CellStyle BackColor="#E8F4FF" HorizontalAlign="Center"></CellStyle> </dx:GridViewDataColumn> <dx:GridViewDataColumn FieldName="CL" Caption="产量" VisibleIndex="2"> <Settings AllowSort="True" /> <HeaderStyle HorizontalAlign="Center" Font-Bold ="true" ForeColor ="white"/> <CellStyle BackColor="#E8F4FF" HorizontalAlign="Center"></CellStyle> </dx:GridViewDataColumn> <dx:GridViewDataColumn FieldName="RJZH" Caption="软件组合" VisibleIndex="3"> <Settings AllowSort="True" /> <HeaderStyle HorizontalAlign="Center" Font-Bold ="true" ForeColor ="white"/> <CellStyle BackColor="#E8F4FF" HorizontalAlign="Center"></CellStyle> </dx:GridViewDataColumn> <dx:GridViewDataColumn FieldName="CLZH" Caption="磁路组合" VisibleIndex="4"> <Settings AllowSort="True" /> <HeaderStyle HorizontalAlign="Center" Font-Bold ="true" ForeColor ="white"/> <CellStyle BackColor="#E8F4FF" HorizontalAlign="Center"></CellStyle> </dx:GridViewDataColumn> </Columns> </dx:ASPxGridView> </td> </tr> </table>
注意点:
画Table时要注意设置runat属性为server。
TextBox里设置属性AutoPostBack为True,这样我们在TextBox修改时就会执行OnTextChanged事件,实现数据检索效果。
ASPXGridView里要设置KeyFieldName(主键)属性。
数据录入事件写在OnRowInserting事件中,数据编辑在OnRowUpdating事件中。
后台Cs:
static string constr = @"Data Source=.;Initial Catalog=MT;Integrated Security=True"; SqlConnection conn = new SqlConnection(constr); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GBinding(); } } private void GBinding() { string TA027 = ""; string TA006 = ""; string TA002 = ""; string sql = ""; if (txtTA027.Text.Trim() != "") { TA027 = string.Format("and TA027=‘{0}‘ ", txtTA027.Text.Trim()); } else { TA027 = ""; } if (txtTA006.Text.Trim() != "") { TA006 = string.Format("and Address=‘{0}‘ ", txtTA006.Text.Trim()); } else { TA006 = ""; } sql += "select * from [Product_Plan] "; sql += "where 1=1 "; sql += TA006+TA027; conn.Open(); DataSet ds = new DataSet(); SqlDataAdapter sda = new SqlDataAdapter(sql, conn); sda.Fill(ds, "Tb1"); DataTable dt = ds.Tables["Tb1"]; ASPxGridView1.DataSource = dt; ASPxGridView1.DataBind(); conn.Close(); } protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e) { string key = Convert.ToString(e.Keys[0]).Trim(); string cl = ""; string rjzh = ""; string clzh = ""; if (e.NewValues["CLZH"] != null) { clzh = e.NewValues["CLZH"].ToString(); } if (e.NewValues["RJZH"] != null) { rjzh = e.NewValues["RJZH"].ToString(); } if (e.NewValues["CL"] != null) { cl = e.NewValues["CL"].ToString(); } string sql = ""; sql = string.Format("UPDATE [Product_Plan] SET [CLZH]=‘{0}‘,[RJZH]=‘{1}‘,[CL]=‘{2}‘ WHERE TA002=‘{3}‘", clzh, rjzh, cl, key); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); this.ASPxGridView1.CancelEdit(); e.Cancel = true; GBinding(); } protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) { string ta002 = ""; string cl = ""; string rjzh = ""; string clzh = ""; string ta027 = ""; string ta006 = ""; if (txtTA027.Text != null) { ta027 = txtTA027.Text.Trim(); } if (txtTA006.Text != null) { ta006 = txtTA006.Text.Trim(); } if (e.NewValues["TA002"] != null) { ta002 = e.NewValues["TA002"].ToString(); } if (e.NewValues["CLZH"] != null) { clzh = e.NewValues["CLZH"].ToString(); } if (e.NewValues["RJZH"] != null) { rjzh = e.NewValues["RJZH"].ToString(); } if (e.NewValues["CL"] != null) { cl = e.NewValues["CL"].ToString(); } string sql = ""; sql = string.Format("INSERT INTO [Product_Plan] (TA002,TA027,TA006,CLZH,RJZH,CL) VALUES(‘{0}‘,‘{1}‘,‘{2}‘,‘{3}‘,‘{4}‘,‘{5}‘)",ta002,ta027,ta006,clzh,rjzh,cl); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); this.ASPxGridView1.CancelEdit(); e.Cancel = true; GBinding(); } protected void ASPxGridView1_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e) { e.NewValues["TA002"] = txtTA002.Text.Trim(); } protected void txtTA027_TextChanged(object sender, EventArgs e) { GBinding(); } protected void ASPxGridView1_PageIndexChanged(object sender, EventArgs e) { GBinding(); } protected void txtTA006_TextChanged(object sender, EventArgs e) { GBinding(); }
效果:
这样就基本实现上述功能。
时间: 2024-09-30 17:46:37