【问题标题】:How to get the values from Access Database and set in Gridview in C# winforms Devexpress?如何从 Access 数据库中获取值并在 C# winforms Devexpress 的 Gridview 中设置?
【发布时间】:2013-11-07 13:34:48
【问题描述】:

我有 Gridview (gridview1),表单中有一些 TextEdits,并将一些数据添加到几行,我将 gridview1 数据和 textedits 存储到 Access 数据库。在另一种形式中,我将一些列绑定到 gridview1 和 TextEdits 到新的 Gridview (gridview2)。现在,如果我单击 gridview2 中任何行上的编辑按钮,我想从 Access 数据库中获取数据并显示在第一个表单 gridview1 中,并且 textedits 会自动填充记录到 Focused Row 单元格的唯一值。

使用此代码,我获得了 TextEdits 字段的价值

OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Srihari/OrionSystem.accdb");

        OleDbCommand da = new OleDbCommand("select * from invoice_top where invoice_number=" + textEdit5.Text, con);
        con.Open();
        OleDbDataReader reader = da.ExecuteReader();
        while (reader.Read())
        {
            textEdit12.Text = reader.GetValue(1).ToString();
            textEdit13.Text = reader.GetValue(2).ToString();
            textEdit4.Text = reader.GetString(3);
            dateEdit1.Text = reader.GetValue(8).ToString();
            textEdit1.Text = reader.GetValue(5).ToString();
            textEdit2.Text = reader.GetValue(6).ToString();
            textEdit3.Text = reader.GetValue(7).ToString();
            checkEdit1.Checked = reader.GetBoolean(4);
         }

        con.Close();

我也想填写 gridview 数据?我试过这辆公共汽车它不工作

gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[2], reader1.GetString(2));

如何将 Access 数据库值设置为 grdiview ?请帮帮我?

提前致谢。

【问题讨论】:

    标签: c# winforms ms-access gridview devexpress


    【解决方案1】:

    我认为您需要阅读有关数据绑定的教程。

    只需设置 GridControl.DataSource 属性,即可将 GridControl 绑定到实现 IList、IBindingList 或 IQueryable 的数据源。无需遍历您的记录集并单独设置每一行/单元格的值。此外,我建议您将 TextEdit 控件数据绑定(到 EditValue 属性,而不是 Text 属性),而不是像上面那样手动设置它们。

    【讨论】:

      【解决方案2】:

      这段代码是VB.NET,但是C#中的ideea是一样的

       Dim SIRCON as string =  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Srihari/OrionSystem.accdb"
      
      Using con As New OleDbConnection(SIRCON)
              con.Open()
              Dim strSQL As String = "select * from invoice_top where invoice_number=" + textEdit5.Text
              dim dt as new datatable
              dt.Load(New OleDbCommand(strSQL, conexiune).ExecuteReader())
              conexiune.Close()
      
              GridControl1.datasource = dt
              GridControl1.ForceInitialise
      
          End Using
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多