根据文本框值向网格添加行 [英] add rows to grid depend upon textbox value
本文介绍了根据文本框值向网格添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码来向数据表添加行。它将从文本框值中获取数字,并将向gridview添加大量行。它的循环正确地循环for循环,但只向网格添加一行。
private void CreateGridViewRows()
{
DataTable dt = new DataTable( );
DataRow dr = null ;
for ( int i = 1 ; i < = int .Parse(txtResources.Text.Trim()); i ++)
{
DataRow drCurrentRow = dt.NewRow();
dt.Columns.Add( new DataColumn( RowNumber, typeof ( string )));
dt.Columns.Add( new DataColumn( Col1, typeof ( string )));
dt.Columns.Add( new DataColumn( Col2, typeof ( string )));
dt.Columns.Add( new DataColumn( Col3, typeof ( string )));
dt.Columns.Add( new DataColumn( Col4, typeof ( string )));
dt.Columns.Add( new DataColumn( Col5, typeof ( string )));
dt.Columns.Add( new DataColumn( Col6, typeof ( string )));
dt.Columns.Add( new DataColumn( Col7, typeof ( string )));
dr = dt.NewRow();
dr [ RowNumber] = 1 跨度>;
dr [ Col1] = string 跨度> .Empty;
dr [ Col2] = string 跨度> .Empty;
dr [ Col3] = string 跨度> .Empty;
dr [ Col4] = string 跨度> .Empty;
dr [ Col5] = string 跨度> .Empty;
dr [ Col6] = string 跨度> .Empty;
dr [ Col7] = string 跨度> .Empty;
dt.Rows.Add(dr);
}
任何建议请求代码更改。提前谢谢。
解决方案
尝试下面
private void CreateGridViewRows()
{
DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn( RowNumber, typeof ( string )));
dt.Columns.Add( new DataColumn( Col1, typeof ( string )));
dt.Columns.Add( new DataColumn( Col2, typeof ( string )));
dt.Columns.Add( new DataColumn( Col3, typeof ( string )));
dt.Columns.Add( new DataColumn( Col4, typeof ( string )));
dt.Columns.Add( new DataColumn( Col5, typeof ( string )));
dt.Columns.Add( new DataColumn( Col6, typeof ( string )));
dt.Columns.Add( new DataColumn( Col7, typeof ( string )));
for ( int i = 1 ; i < = int .Parse(txtResources.Text.Trim()); i ++)
{
DataRow dr = dt.NewRow();
dr [ RowNumber] = i;
dr [ Col1] = string 跨度> .Empty;
dr [ Col2] = string 跨度> .Empty;
dr [ Col3] = string 跨度> .Empty;
dr [ Col4] = string 跨度> .Empty;
dr [ Col5] = string 跨度> .Empty;
dr [ Col6] = string 跨度> .Empty;
dr [ Col7] = string 跨度> .Empty;
dt.Rows.Add(dr);
}
这不是创建数据表的正确方法
你穿上写侧面循环添加栏目
尝试这个它会起作用
< pre lang =C#> private void CreateGridViewRows()
{
DataTable dt = new DataTable();
DataRow dr = null ;
dt.Columns.Add( new DataColumn( RowNumber, typeof ( string )));
dt.Columns.Add( new DataColumn( Col1, typeof ( string )));
dt.Columns.Add( new DataColumn( Col2, typeof ( string )));
dt.Columns.Add( new DataColumn( Col3, typeof ( string )));
dt.Columns.Add( new DataColumn( Col4, typeof ( string )));
dt.Columns.Add( new DataColumn( Col5, typeof ( string )));
dt.Columns.Add( new DataColumn( Col6, typeof ( string )));
dt.Columns.Add( new DataColumn( Col7, typeof ( string )));
for ( int i = 1 ; i < =( 3 ); i ++)
{
DataRow drCurrentRow = dt.NewRow();
dr = dt.NewRow();
dr [ RowNumber] = 1 跨度>;
dr [ Col1] = string 跨度> .Empty;
dr [ Col2] = string 跨度> .Empty;
dr [ Col3] = string 跨度> .Empty;
dr [ Col4] = string 跨度> .Empty;
dr [ Col5] = string 跨度> .Empty;
dr [ Col6] = string 跨度> .Empty;
dr [ Col7] = string 跨度> .Empty;
dt.Rows.Add(dr);
}
I have following code to add rows to the datatable. It will take the number from textbox value and will add that much number of rows to the gridview. Its looping correctly thr for loop but adding only one row to grid.
private void CreateGridViewRows()
{
DataTable dt = new DataTable();
DataRow dr = null;
for (int i = 1; i <= int.Parse(txtResources.Text.Trim()); i++)
{
DataRow drCurrentRow = dt.NewRow();
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dt.Columns.Add(new DataColumn("Col3", typeof(string)));
dt.Columns.Add(new DataColumn("Col4", typeof(string)));
dt.Columns.Add(new DataColumn("Col5", typeof(string)));
dt.Columns.Add(new DataColumn("Col6", typeof(string)));
dt.Columns.Add(new DataColumn("Col7", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] =1;
dr["Col1"] = string.Empty;
dr["Col2"] = string.Empty;
dr["Col3"] = string.Empty;
dr["Col4"] = string.Empty;
dr["Col5"] = string.Empty;
dr["Col6"] = string.Empty;
dr["Col7"] = string.Empty;
dt.Rows.Add(dr);
}
Any suggestions pleas eto code changes.Thanks in advance.
解决方案
try below
private void CreateGridViewRows() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("RowNumber", typeof(string))); dt.Columns.Add(new DataColumn("Col1", typeof(string))); dt.Columns.Add(new DataColumn("Col2", typeof(string))); dt.Columns.Add(new DataColumn("Col3", typeof(string))); dt.Columns.Add(new DataColumn("Col4", typeof(string))); dt.Columns.Add(new DataColumn("Col5", typeof(string))); dt.Columns.Add(new DataColumn("Col6", typeof(string))); dt.Columns.Add(new DataColumn("Col7", typeof(string))); for (int i = 1; i <= int.Parse(txtResources.Text.Trim()); i++) { DataRow dr = dt.NewRow(); dr["RowNumber"] =i; dr["Col1"] = string.Empty; dr["Col2"] = string.Empty; dr["Col3"] = string.Empty; dr["Col4"] = string.Empty; dr["Col5"] = string.Empty; dr["Col6"] = string.Empty; dr["Col7"] = string.Empty; dt.Rows.Add(dr); }
Hi,
This is not right way to create data table
you shod write add column out side loop
try this it will work
private void CreateGridViewRows() { DataTable dt = new DataTable(); DataRow dr = null; dt.Columns.Add(new DataColumn("RowNumber", typeof(string))); dt.Columns.Add(new DataColumn("Col1", typeof(string))); dt.Columns.Add(new DataColumn("Col2", typeof(string))); dt.Columns.Add(new DataColumn("Col3", typeof(string))); dt.Columns.Add(new DataColumn("Col4", typeof(string))); dt.Columns.Add(new DataColumn("Col5", typeof(string))); dt.Columns.Add(new DataColumn("Col6", typeof(string))); dt.Columns.Add(new DataColumn("Col7", typeof(string))); for (int i = 1; i <= (3); i++) { DataRow drCurrentRow = dt.NewRow(); dr = dt.NewRow(); dr["RowNumber"] = 1; dr["Col1"] = string.Empty; dr["Col2"] = string.Empty; dr["Col3"] = string.Empty; dr["Col4"] = string.Empty; dr["Col5"] = string.Empty; dr["Col6"] = string.Empty; dr["Col7"] = string.Empty; dt.Rows.Add(dr); }
这篇关于根据文本框值向网格添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文