posistion没有排[0]

时间:2016-05-14 08:15:56

标签: c# sql datatable datarow

我有以下代码:

Top executives Chief executives 11-1011 \n
Top executives General and operations managers 11-1021 \n
Top executives Legislators  11-1031 \n

当我运行表单时,此代码会抛出错误:

  

位置0没有行。

但是当我使用datagridview时:

private void ReturnForm_Load(object sender, EventArgs e)
{
        string select = "SELECT TransactionID FROM [Transaction] WHERE ReturnDate = '" + null + "'";
        comboBox1.DisplayMember = "TransactionID";
        comboBox1.ValueMember = "TransactionID";
        comboBox1.DataSource = con.FillTable(select);
        comboBox1.SelectedIndex = -1;
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
        int rentduration;
        int lateduration;
        double extracharge;
        int totalprice;
        int price;

        string a = "SELECT StaffID,CarID,ClientName,StartDate,EndDate FROM [Transaction] WHERE TransactionID = '" + comboBox1.Text + "'";
        string b = "SELECT * FROM MsCar WHERE CarrID = '" + label12.Text + "'";

        DataTable dt = con.FillTable(a);
        DataTable da = con.FillTable(b);
        dataGridView1.DataSource = dt;

            label10.Text = dt.Rows[0][1].ToString();
            label11.Text = dt.Rows[0][3].ToString();
            label12.Text = dt.Rows[0][2].ToString();
            label13.Text = dt.Rows[0][4].ToString();
            label14.Text = dt.Rows[0][5].ToString();
            label15.Text = DateTime.Now.ToString("MM dd YYYY");
}

显示所有数据。

请帮帮我。

1 个答案:

答案 0 :(得分:1)

尝试在提交后使用组合框选择组合框在关闭表单时也会发生选择的索引更改,并且选定的索引更改事件函数与空字符串一起使用。在将数据发送到SqlDataAdapter时也会删除空格。

使用

private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)

而不是

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

 string a = "SELECT StaffID,CarID,ClientName,StartDate,EndDate FROM [Transaction] WHERE TransactionID = '" + comboBox1.Text.Trim() + "'";