【问题标题】:How I Can Show More Table In Datagridview C#如何在 Datagridview C# 中显示更多表格
【发布时间】:2015-12-28 23:57:03
【问题描述】:

我可以使用 c# 和 mysql 按日期排序在一个 datagridview 中显示两个或多个表吗?

例如:


| table1.salesNo | table1.salesMoney | table1.date1 |

| table2.purchNo | table2.purchMoney | table2.date2 |

| table2.purchNo | table2.purchMoney | table2.date3 |

| table1.salesNo | table1.salesMoney | table1.date4 |

| table1.salesNo | table1.salesMoney | table1.date5 |


我使用了这段代码,但没有数据出现

 private MySqlDataAdapter salesinvoices, purchasesinvoices;
        private DataSet jedataset;
private void button2_Click(object sender, EventArgs e)
      {
          const string SELECT_salesinvoices = "SELECT * FROM sales_invoices";
          const string SELECT_purchasesinvoices = "SELECT * FROM purchase_invoices";



          // Compose the connection string.
          string connect_string = Publics.je_Coonn;

          // Create a DataAdapter to load the Addresses table.
          salesinvoices = new MySqlDataAdapter(SELECT_salesinvoices,
              connect_string);

          // Create a DataAdapter to load the Addresses table.
          purchasesinvoices = new MySqlDataAdapter(SELECT_purchasesinvoices,
              connect_string);

          // Create and fill the DataSet.
          jedataset = new DataSet("je_coronasalesdbDataSet");
          salesinvoices.Fill(jedataset, "sales_invoices");
          purchasesinvoices.Fill(jedataset, "purchase_invoices");

          // Bind the DataGrid to the DataSet.
          dataGridView1.DataSource = jedataset;
      }

谢谢

【问题讨论】:

  • 在设置DataSource之后看起来你需要dataGridView1.DataBind();
  • 谢谢,我试过了,但出现此错误 System.Windows.Forms.DataGridView' 不包含'DataBind' 的定义,并且没有扩展方法'DataBind' 接受类型的第一个参数
  • DataBind() 不接受任何参数。尝试完全按照我在上一条评论中输入的方式添加该行。
  • 我做了那个 dataGridView1.DataBind();正是

标签: c# mysql


【解决方案1】:

据我所知 DataBind() 仅适用于 Web 表单。尝试只绑定属性 .DataSource。以防万一仍然不起作用,请尝试 .Refresh()

dataGridView1.DataSource = jedataset;
dataGridView1.Refresh();

编辑:如果您仍然看不到任何数据,请添加:

dataGridView1.AutoGenerateColumns = true

(我不知道你是否已经添加了列)

编辑2:找到这个代码here,我没有测试过,但是你可以修改它并尝试是否有效:

private static void DemonstrateMergeTable()
{
    DataTable table1 = new DataTable("Items");

    // Add columns
    DataColumn column1 = new DataColumn("id", typeof(System.Int32));
    DataColumn column2 = new DataColumn("item", typeof(System.Int32));
    table1.Columns.Add(column1);
    table1.Columns.Add(column2);

    // Set the primary key column.
    table1.PrimaryKey = new DataColumn[] { column1 };



    // Add some rows.
    DataRow row;
    for (int i = 0; i <= 3; i++)
    {
        row = table1.NewRow();
        row["id"] = i;
        row["item"] = i;
        table1.Rows.Add(row);
    }

    // Accept changes.
    table1.AcceptChanges();
    PrintValues(table1, "Original values");

    // Create a second DataTable identical to the first.
    DataTable table2 = table1.Clone();

    // Add three rows. Note that the id column can't be the  
    // same as existing rows in the original table.
    row = table2.NewRow();
    row["id"] = 14;
    row["item"] = 774;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 12;
    row["item"] = 555;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 13;
    row["item"] = 665;
    table2.Rows.Add(row);

    // Merge table2 into the table1.
    Console.WriteLine("Merging");
    table1.Merge(table2);
    PrintValues(table1, "Merged With table1");

}

【讨论】:

  • ,, 没有数据出现,,, dataGridView1 没有任何列
  • 好的,试试这个:dataGridView1.DataSource = jedataset.Tables[0]; @SunEye
  • 只显示sales_invoices
  • 这是因为 DataSet 是 DataTables 的集合,并且您有 2 个表(0 和 1)。 0 = 销售,1 = 购买。您尝试做的事情有点复杂且不推荐,因为 DataGridView 旨在仅处理一个数据源。据我所知,将两个 DataTable 合二为一是不可能的。你可以做是创建一个包含所有购买字段和所有销售字段的自定义对象,将它们解析为自定义对象类型和然后列出所有以最终将 自定义对象列表 与 dataGridView 绑定。您将处理 1 个数据源 @SunEye
  • 当前上下文中不存在名称“PrintValues”,如何创建包含所有采购字段和所有销售字段的自定义对象以及如何将两个表 1 和表 2 与 datagridview 绑定
猜你喜欢
  • 1970-01-01
  • 2013-05-22
  • 2011-02-16
  • 1970-01-01
  • 2023-01-12
  • 2014-04-16
  • 2011-01-14
  • 1970-01-01
  • 2021-06-14
相关资源
最近更新 更多