Reputation: 399
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;
}
}
Upvotes: 0
Views: 834
Reputation: 1118
Don't bother making lists in the first place. Just load a table directly.
DataTable myData = new DataTable();
DataColumn seqCol = new DataColumn("Seq", typeof (int));
DataColumn maxLenCol = new DataColumn("MaxLen", typeof (int));
myData.Columns.Add(seqCol);
myData.Columns.Add(maxLenCol);
while (reader.Read())
{
var row = myData.NewRow();
bool addRow = false;
int seq;
if (int.TryParse(reader["Seq"].ToString(), out seq))
if (seq > 30)
{
row[seqCol] = seq;
addRow = true;
}
int maxlen;
if (int.TryParse(reader["MaxLen"].ToString(), out maxlen))
if (maxlen > 30.00)
{
row[maxLenCol] = maxlen;
addRow = true;
}
if (addRow)
{
myData.Rows.Add(row);
}
}
Then bind this table to a grid
DataGridView dgv = new DataGridView();
dgv.DataSource = myData;
Upvotes: 0
Reputation: 1174
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;
}
Upvotes: 1