批量获取DataGrid控件模板列中的数据

批量获取DataGrid控件模板列中的数据

在DataGrid中一般只能单个获取每一行的数据,若要批量获取DataGrid控件中的数据必须对每一个模板列的控件进行扫描,获取其中的数据。
我本想做的程序是根据不同的行数,由用户一次输入若干数据,提交后系统自动获取批量数据的程序。
以下程序简单表达了需要实现的功能

test.aspx

..........

 1<asp:datagrid autogeneratecolumns="False" backcolor="#DEBA84" bordercolor="#DEBA84" borderstyle="None" borderwidth="1px" cellpadding="3" cellspacing="2" id="dgResult" runat="server">
 2<footerstyle backcolor="#F7DFB5" forecolor="#8C4510"></footerstyle>
 3<selecteditemstyle backcolor="#738A9C" font-bold="True" forecolor="White"></selecteditemstyle>
 4<itemstyle backcolor="#FFF7E7" forecolor="#8C4510"></itemstyle>
 5<headerstyle backcolor="#A55129" font-bold="True" forecolor="White"></headerstyle>
 6<columns>
 7<asp:boundcolumn datafield="id" headertext="列号"></asp:boundcolumn>
 8<asp:templatecolumn headertext="列名">
 9<itemtemplate>
10<asp:textbox enabled="True" id="col" runat="server" width="50"></asp:textbox>
11</itemtemplate>
12</asp:templatecolumn>
13<asp:templatecolumn headertext="整数精度">
14<itemtemplate>
15<asp:textbox enabled="True" id="Textbox1" runat="server" width="50">20</asp:textbox>
16</itemtemplate>
17</asp:templatecolumn>
18<asp:templatecolumn headertext="小数点精度">
19<itemtemplate>
20<asp:textbox enabled="True" id="Textbox2" runat="server" width="50">10</asp:textbox>
21</itemtemplate>
22</asp:templatecolumn>
23</columns>
24<pagerstyle forecolor="#8C4510" horizontalalign="Center" mode="NumericPages"></pagerstyle>
25</asp:datagrid>
1<asp:button id="btnOK" runat="server" text="提交"></asp:button>

......

test.aspx.cs

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
mfbind(DataSource());
}

private IList DataSource()
{
DataTable dt=new DataTable();
DataColumn dc=new DataColumn();
dc.ColumnName="id";
dc.DataType=System.Type.GetType("System.Int32");
dc.ReadOnly=true;
dc.Unique=true;
dc.AutoIncrement=true;
dc.AutoIncrementSeed=0;
dc.AutoIncrementStep=1;
dt.Columns.Add(dc);

dc=new DataColumn();
dc.ColumnName="列名称";
dc.DataType=System.Type.GetType("System.String");
dt.Columns.Add(dc);

for(int i=0;i<10;i++)
{
DataRow dr=dt.NewRow();
dr[1]=i;
dt.Rows.Add(dr);
}
Session["Source"] = dt;
return dt.DefaultView;
}
private void mfbind(IList dv)
{
this.dgResult.DataSource=(DataView)dv;
this.dgResult.DataBind();
}
private void btnOK_Click(object sender, System.EventArgs e)
{
//string tmpa=dgResult__ctl2_col1.Text;
TextBox txt;
ArrayList mArr=new ArrayList();
for(int i=0;i<10;i++)
{
txt=new TextBox();
txt=(TextBox)dgResult.Items[i].FindControl("col");
mArr.Add(txt.Text);
}
for(int i=0;i<mArr.Count;i++)
this.lblProblem.Text+=mArr[i].ToString()+" ; ";

}

其实这样的程序有共通性,通过DataGrid控件可以对数据进行批量处理,特别是对删除数据等操作的过程中使用起来及其方便快捷,只要将程序的模板列中的TextBox控件改为CheckBox控件或者DropDownList控件,扫描所有的子控件就可以实现对数据的批量快速删除、修改等操作。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus