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

时间:2021-02-08 15:38:52

I need your help. I want to show all data based on number I input in textbox. Number that is inputted in textbox will be my threshold. I want to show all data in table "Reject" that more than my threshold to datagridview. Can anyone help me ? This is my code :

我需要你的帮助。我想根据文本框中输入的数字I显示所有的数据。在文本框中输入的数字将是我的阈值。我想要显示表“拒绝”中的所有数据,超过我对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];
    }
}

Can anyone help me ? I confused how I can show only data that more than my threshold. Thank you

有人能帮我吗?我搞不懂如何只显示超过阈值的数据。谢谢你!

1 个解决方案

#1


1  

Try this:

试试这个:

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;
}

#1


1  

Try this:

试试这个:

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;
}