基本上我有一个c#应用程序,它从文本文件中获取值,将它们存储在列表中并将它们传输到SQLite表中。
这三个表分别命名为abc,t2和t3。
然后我想接受那些sqlite表并将它们呈现在datagridviews中的c#app中。 到目前为止,我已经能够将1个表(表abc)传输到网格视图。 我想将所有3个表格转移到gridview。
问题:我想让3个表格出现在一个网格中,但我根本不知道如何完成该任务的语法。
以下是如何填充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();
答案 0 :(得分: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
那些);但算法很清楚,你不应该找到任何问题来适应你的特定需求。
答案 1 :(得分:1)
答案取决于您希望如何格式化您要显示的数据。我假设你想要像[abc-col1] [abc-col2] [t2-col1] ...... [t3-col3]
快速而肮脏的方法是使用'datarow'并将数据合并到新的数据表中;
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源。
希望有所帮助。欢呼声。