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

时间:2016-11-11 00:52:31

标签: c# excel datagridview

我需要你的帮助。我想根据文本框中输入的数字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];
    }
}

任何人都可以帮助我吗?我很困惑,我只能显示超过我的阈值的数据。谢谢

1 个答案:

答案 0 :(得分:1)

试试这个:

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