使用Excel (oledb)和c#显示文本框中输入的数据到dataGridView

本文关键字:输入 数据 dataGridView 文本 显示 Excel oledb 使用 | 更新日期: 2023-09-27 18:17:26

我需要你的帮助。我想显示基于我在文本框中输入的数字的所有数据。在文本框中输入的数字将是我的阈值。我想在表"拒绝"中显示所有数据,超过我的阈值到datagridview。有人能帮我吗?这是我的代码:

if (NominalBox.Text != "")
{
    int thresholdcas50;
    Int32.TryParse(NominalBox.Text, out thresholdcas50); 
    koneksi.Open();
    System.Data.DataTable aksesdatatabel;
    aksesdatatabel = koneksi.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    koneksi.Close();
    OleDbCommand command = new OleDbCommand
    (
        "select Reject from [Sheet1$]", koneksi
    );
    DataSet coba = new DataSet();
    OleDbDataAdapter adapter = new OleDbDataAdapter(command);
    adapter.Fill(coba);
    // Here I want to read all datas in "Reject" and convert
    // them into integer. There is an error here.
    // It says "Input string was not in a correct format".
    int x = int.Parse(coba.Tables[0].ToString());
    if (x > thresholdcas50)
    {
        // I stuck here. I don't know how to show all datas that
        // more than my threshold.
        dataGridView1.DataSource = coba.Tables[0];
    }
}

有人能帮我吗?我很困惑如何才能只显示超出阈值的数据。谢谢你

使用Excel (oledb)和c#显示文本框中输入的数据到dataGridView

试试这个:

if (NominalBox.Text != "")
{
    int thresholdcas50;
    Int32.TryParse(NominalBox.Text, out thresholdcas50);
    koneksi.Open();
    System.Data.DataTable aksesdatatabel;
    aksesdatatabel = koneksi.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    koneksi.Close();

    OleDbCommand command = new OleDbCommand
    (
        "select Reject from [Sheet1$]", koneksi
    );
    DataSet coba = new DataSet();
    OleDbDataAdapter adapter = new OleDbDataAdapter(command);
    adapter.Fill(coba);
    // Just made a variable to quick identify your table.
    var table = coba.Tables[0];
    // Make a view from your table.
    var view = new DataView(table);
    // Make a filter on the view.
    view.RowFilter = string.Format("Reject > {0}", thresholdcas50);
    // Now set the DataSource to your filter.
    dataGridView1.DataSource = view;
}