0

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;
               }



       }
4

2 回答 2

1

我会创建一个适配器类来获取您的多个列表并填充自定义对象或数据表。然后,您可以将该对象作为数据源绑定到您的网格。

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;
}
于 2013-11-14T16:11:59.803 回答
0

首先不要费心制作清单。直接加载表即可。

  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);
    }
  }

然后将此表绑定到网格

 DataGridView dgv = new DataGridView();
  dgv.DataSource = myData;
于 2013-11-15T18:21:37.750 回答