如何使用列表值填充datagrid作为c#中的单独列 [英] How to populate datagrid with list values as separate columns in c#
问题描述
我如何填充多个列表的数据网格视图?基本上,列1应该是list1,列2,列表2等。
编辑:有人认为我应该使用列表视图吗?如果是这样,那么如何?
这是我的解析代码,以获取我的列表的值。现在我需要用这些列表填充一个DGV。
string sql4 =select * from abc;
SQLiteCommand command = new SQLiteCommand(sql4,sqlite_conn);
// datareader允许我们按行读取表abc行
SQLiteDataReader reader = command.ExecuteReader();
//接下来会发生什么:我们正在尝试解析每列不规则以向用户显示。例如,如果列
// Seq中的值> 30,我们需要让用户知道。我们通过将所有值> 30添加到SeqIrregularities列表来实现。
while(reader.Read())
{
int seq;
if(int.TryParse(reader [Seq]。ToString(),out seq))
if(seq> 30)
{
SeqIrregularities 。添加(seq);
seq1 = true;
}
int maxlen;
if(int.TryParse(reader [MaxLen]。ToString(),out maxlen))
if(maxlen> 30.00)
{
MaxLen。添加(maxlen);
maxlen1 = true;
}
}
我将创建一个适配器类来取代您的多个列表,并填充一个自定义对象或者一个datatable。然后,您可以将该对象作为数据源绑定到网格中。
public DataTable ConvertListsToDatatable(List< int> list1,List< int& ; list2)
{
DataTable dt = new DataTable();
DataColumn列;
DataRow行;
//添加第一列
column = new DataColumn();
column.DataType = System.Type.GetType(System.Int32);
column.ColumnName =List1Id;
dt.Columns.Add(column);
//添加第二列
column = new DataColumn();
column.DataType = System.Type.GetType(System.Int32);
column.ColumnName =List2Id;
dt.Columns.Add(column);
int i = 0;
while((list1!= null)&(i< list1.Count)||(list2!= null)&&(i< list2.Count))
{
row = dt.NewRow();
if(list1!= null)
{
if(i< list1.Count)
{
row [List1Id] = List1 [一世];
}
}
if(list2!= null)
{
if(i< list2.Count)
{
row [List2Id] = List2 [i];
}
}
dt.Rows.Add(row);
i ++;
}
return dt;
}
I already have an SQlite database setup. Right now I'm parsing through it. For example, if certain values in the column Seq are >30 I transfer those values to a list. I want to use that list, to populate a datagrid view so the user can see what values were > 30
How do I populate a data grid view with multiple lists? Basically column 1 should be list1, column 2, list 2, etc.
EDIT: DOES anyone think I should use a list view instead? If so, how?
Here's my code for parsing to obtain values for my lists. Now I need to somehow populate a DGV with these lists.
string sql4 = "select * from abc";
SQLiteCommand command = new SQLiteCommand(sql4, sqlite_conn);
// The datareader allows us to read the table abc row by row
SQLiteDataReader reader = command.ExecuteReader();
// What happens next: We are trying to parse each column for irregularities to show to the user. For example if a value in column
// Seq is >30, we need to let the user know. We do this by adding all values >30 to the SeqIrregularities list.
while (reader.Read())
{
int seq;
if (int.TryParse(reader["Seq"].ToString(), out seq))
if (seq > 30)
{
SeqIrregularities.Add(seq);
seq1 = true;
}
int maxlen;
if (int.TryParse(reader["MaxLen"].ToString(), out maxlen))
if (maxlen> 30.00)
{
MaxLen.Add(maxlen);
maxlen1 = true;
}
}
I'd create an adapter class to take your multiple lists and populate either a custom object or perhaps a datatable. Then you can bind that object as the datasource to your grid.
public DataTable ConvertListsToDatatable(List<int> list1, List<int> list2)
{
DataTable dt = new DataTable();
DataColumn column;
DataRow row;
// add the first column
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "List1Id";
dt.Columns.Add(column);
// add the second column
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "List2Id";
dt.Columns.Add(column);
int i = 0;
while ((list1 != null)&&(i < list1.Count) || (list2 != null)&&(i < list2.Count))
{
row = dt.NewRow();
if (list1 != null)
{
if (i < list1.Count)
{
row["List1Id"] = List1[i];
}
}
if (list2 != null)
{
if (i < list2.Count)
{
row["List2Id"] = List2[i];
}
}
dt.Rows.Add(row);
i++;
}
return dt;
}
这篇关于如何使用列表值填充datagrid作为c#中的单独列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!