0

基本上我有 ac# 应用程序,它从文本文件中获取值,将它们存储在列表中并将它们传输到 SQLite 表中。

这三个表分别命名为 abc、t2 和 t3。

然后我想获取这些 sqlite 表并将它们呈现在 datagridviews 的 c# 应用程序中。到目前为止,我已经能够将 1 个表(表 abc)传输到网格视图。 我想将所有 3 个表转移到 gridview。

问题:我想让 3 个表格出现在 ONE 网格中,但我根本不知道如何做到这一点的语法。

以下是这 3 个表的填充方式。更重要的代码(将数据传输到网格)在此下方

    // TRANSFERING VALUES TO THE SQLITE DB

    private void button4_Click(object sender, EventArgs e)
    {
          // We use these three SQLite objects:
          SQLiteConnection sqlite_conn;
          SQLiteCommand sqlite_cmd;
          SQLiteDataReader sqlite_datareader;

          // create a new database connection: // Maybe error here - video was different
          sqlite_conn = new SQLiteConnection(@"Data Source=database.db;Version=3;");

          // open the connection:
          sqlite_conn.Open();

          // create a new SQL command:
          sqlite_cmd = sqlite_conn.CreateCommand();

          // Let the SQLiteCommand object know our SQL-Query:
          sqlite_cmd.CommandText = "CREATE TABLE IF NOT EXISTS 'abc' (Seq text, Field text, Desc text, Len text, Dec text, Typ text, Percnt text, Pop text, Alzero text, MaxLen text );";

          // Now lets execute the SQL                                                                                 
          sqlite_cmd.ExecuteNonQuery();

          // **** SQLITE TRANSFER SECTION 1 - transfer values from list1 to table1 *****

          sqlite_cmd.CommandText = " DELETE FROM abc";
          sqlite_cmd.ExecuteNonQuery();

          sqlite_cmd.CommandText = "INSERT INTO abc (Seq, Field, Desc, Len, Dec, Typ, Percnt, Pop, Alzero, MaxLen) VALUES (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)";
          sqlite_cmd.Parameters.AddWithValue("@p1", 6);  // dummy initial values 
          sqlite_cmd.Parameters.AddWithValue("@p2", 878); 
          sqlite_cmd.Parameters.AddWithValue("@p3", 56);
          sqlite_cmd.Parameters.AddWithValue("@p4", 6);
          sqlite_cmd.Parameters.AddWithValue("@p5", 546);
          sqlite_cmd.Parameters.AddWithValue("@p6", 565);
          sqlite_cmd.Parameters.AddWithValue("@p7", 568);
          sqlite_cmd.Parameters.AddWithValue("@p8", 526);
          sqlite_cmd.Parameters.AddWithValue("@p9", 586);
          sqlite_cmd.Parameters.AddWithValue("@p10", 526);


          for (int i = 0; i < NumListValues; i += 10) // Filling SQlite table rows and columns with values from our list 
          {
              sqlite_cmd.Parameters.AddWithValue("@p1", list[i]);
              sqlite_cmd.Parameters.AddWithValue("@p2", list[i+1]);
              sqlite_cmd.Parameters.AddWithValue("@p3", list[i+2]);
              sqlite_cmd.Parameters.AddWithValue("@p4", list[i+3]);
              sqlite_cmd.Parameters.AddWithValue("@p5", list[i+4]);
              if (i > 490)
                  break; 
              sqlite_cmd.Parameters.AddWithValue("@p6", list[i+5]);
              sqlite_cmd.Parameters.AddWithValue("@p7", list[i+6]);
              sqlite_cmd.Parameters.AddWithValue("@p8", list[i+7]);
              sqlite_cmd.Parameters.AddWithValue("@p9", list[i+8]);
              sqlite_cmd.Parameters.AddWithValue("@p10", list[i+9]);
              sqlite_cmd.ExecuteNonQuery();

          }

          //   **** SQLITE TRANSFER SECTION 2 - transfer values from list2 to 2nd table *****

          sqlite_cmd.CommandText = "CREATE TABLE IF NOT EXISTS 't2' (YYMM text, MinDate text, MaxDate text, TotalTrans text, DebitTrans text, AMOUNTINDOCUMENTCURREN text );";

          sqlite_cmd.ExecuteNonQuery();

          sqlite_cmd.CommandText = " DELETE FROM t2";
          sqlite_cmd.ExecuteNonQuery();

          sqlite_cmd.CommandText = "INSERT INTO t2 (YYMM, MinDate, MaxDate, TotalTrans, DebitTrans, AMOUNTINDOCUMENTCURREN ) VALUES (@b1, @b2, @b3, @b4, @b5, @b6)";
          sqlite_cmd.Parameters.AddWithValue("@b1", 6);  // dummy initial values 
          sqlite_cmd.Parameters.AddWithValue("@b2", 878); 
          sqlite_cmd.Parameters.AddWithValue("@b3", 56);
          sqlite_cmd.Parameters.AddWithValue("@b4", 6);
          sqlite_cmd.Parameters.AddWithValue("@b5", 546);
          sqlite_cmd.Parameters.AddWithValue("@b6", 565);

         for (int i = 0; i < NumList2Values; i+= 6) // Filling SQlite table rows and columns with values from list2
         {
             sqlite_cmd.Parameters.AddWithValue("@b1", list2[i]);
             sqlite_cmd.Parameters.AddWithValue("@b2", list2[i+1]);
             sqlite_cmd.Parameters.AddWithValue("@b3", list2[i+2]);
             sqlite_cmd.Parameters.AddWithValue("@b4", list2[i+3]);
             sqlite_cmd.Parameters.AddWithValue("@b5", list2[i+4]);
             sqlite_cmd.Parameters.AddWithValue("@b6", list2[i+5]);
             sqlite_cmd.ExecuteNonQuery();
         }



        // Create table to transfer values from list 3

       sqlite_cmd.CommandText = "CREATE TABLE IF NOT EXISTS 't3' (YYWW text, MinDate text, MaxDate text, TotalTrans text, DebitTrans text, AMOUNTINDOCUMENTCURREN text );";

       sqlite_cmd.ExecuteNonQuery();

       sqlite_cmd.CommandText = " DELETE FROM t3";
       sqlite_cmd.ExecuteNonQuery();

       sqlite_cmd.CommandText = "INSERT INTO t3 (YYWW, MinDate, MaxDate, TotalTrans, DebitTrans, AMOUNTINDOCUMENTCURREN ) VALUES (@c1, @c2, @c3, @c4, @c5, @c6)";
       sqlite_cmd.Parameters.AddWithValue("@c1", 6);  // dummy initial values 
       sqlite_cmd.Parameters.AddWithValue("@c2", 878); 
       sqlite_cmd.Parameters.AddWithValue("@c3", 56);
       sqlite_cmd.Parameters.AddWithValue("@c4", 6);
       sqlite_cmd.Parameters.AddWithValue("@c5", 546);
       sqlite_cmd.Parameters.AddWithValue("@c6", 565);

       for (int i = 0; i < NumList3Values ; i+= 6) // Filling SQlite table rows and columns with values from list2
       {
           sqlite_cmd.Parameters.AddWithValue("@c1", list3[i]);
           sqlite_cmd.Parameters.AddWithValue("@c2", list3[i+1]);
           sqlite_cmd.Parameters.AddWithValue("@c3", list3[i+2]);
           sqlite_cmd.Parameters.AddWithValue("@c4", list3[i+3]);
           sqlite_cmd.Parameters.AddWithValue("@c5", list3[i+4]);
           sqlite_cmd.Parameters.AddWithValue("@c6", list3[i+5]);
           sqlite_cmd.ExecuteNonQuery();

       }

以下是我如何使用第一个表中的值填充数据网格

       string sql = " SELECT * FROM abc";
       SQLiteDataAdapter da;
       DataTable dt;

       sqlite_cmd.CommandText = sql;

       //Create New Datatable to fill with data
       dt = new DataTable();

       //Create DataAdapter to fill data in DataTable via Adapter
       da = new SQLiteDataAdapter(sql, sqlite_conn);
       da.Fill(dt);

       // Lets populate the datagrid
       dataGridView1.DataSource = dt;
       dataGridView1.Refresh();
4

2 回答 2

2

DataGridView只能处理一张桌子,因此您必须将所有桌子组合成一张。您有多种选择来执行此操作,并且还取决于您希望如何准确地执行合并。示例代码:

DataTable allDTs = abc;
int rowCount = allDTs.Rows.Count - 1;
foreach (DataRow row in t2.Rows)
{
    rowCount = rowCount + 1;
    allDTs.Rows.Add();

    int colCount = -1;
    foreach (DataColumn col in t2.Columns)
    {
        colCount = colCount + 1;
        if (colCount > allDTs.Columns.Count - 1) allDTs.Columns.Add(col.ColumnName);
        allDTs.Rows[rowCount][colCount] = row[col];
    }
}

rowCount = allDTs.Rows.Count - 1;
foreach (DataRow row in t3.Rows)
{
    rowCount = rowCount + 1;
    allDTs.Rows.Add();

    int colCount = -1;
    foreach (DataColumn col in t3.Columns)
    {
        colCount = colCount + 1;
        if (colCount > allDTs.Columns.Count - 1) allDTs.Columns.Add(col.ColumnName);
        allDTs.Rows[rowCount][colCount] = row[col];
    }
}

dataGridView1.DataSource = allDTs;

此代码来自假设您希望将每个列的所有列一个DataTable接一个地放置(所有abc列,然后t2/t3个);但是该算法非常清晰,您不应该发现任何问题来适应您的特定需求。

于 2013-11-11T21:20:09.817 回答
1

对此的答案取决于您希望如何格式化您尝试显示的数据。我假设您想要类似 [abc-col1][abc-col2][t2-col1] ...[t3-col3]

一种快速而肮脏的方法是使用“数据行”并将您认为适合的数据组合到新的数据表中;

datatable combinetable = new datatable();

for (i=0;i<abc.rows.count;i++)
{
for (j=0;j<abc.columns.count;j++)
{
datarow newdatarow = new datarow();
newdatarow[j] = abc[i,j];



}
 combinetable.rows.add(newdatarow);
}


for (i=0;i<t2.rows.count;i++)
{
for (j=0;j<t2.columns.count;j++)
{
datarow newdatarow = new datarow();
newdatarow[j] = t2[i,j];



}
combinetable.rows.add(newdatarow);
}

只需将 [i,j] 部分替换为要组合的实际值的索引,就完成了!如果每个数据表有多个列,您可能需要添加另一个循环,我想您会这样做。创建新的组合数据表后,只需像上面那样将其设置为 datagridview 源。

希望有帮助。干杯。

于 2013-11-11T21:04:08.270 回答