选择datagridview行值并将其传递给另一个表单

时间:2014-11-17 16:25:30

标签: c# datagridview

在我的应用程序中,我想在datagridview中选择一个完整的行并将其传递给另一个表单中的组合框。我尝试使用下面的代码,但它给我一个错误“没有重载方法'这个'需要1个参数”

这是我的代码

if(dataGridView1.SelectedCells.Count > 0)
{
    var oneCell = dataGridView1[0];
    int editloannumber = int.Parse(dataGridView1.Rows[oneCell.RowIndex].Cells[0].Value.ToString());
    int editlid = int.Parse(dataGridView1.Rows[oneCell.RowIndex].Cells[8].Value.ToString());
    string editloantype = dataGridView1.Rows[oneCell.RowIndex].Cells[1].Value.ToString();
    string editneworsecond = dataGridView1.Rows[oneCell.RowIndex].Cells[2].Value.ToString();
    string editpurpose = dataGridView1.Rows[oneCell.RowIndex].Cells[11].Value.ToString();
    string editstatus = dataGridView1.Rows[oneCell.RowIndex].Cells[3].Value.ToString();
    string editcomments = dataGridView1.Rows[oneCell.RowIndex].Cells[7].Value.ToString();
    string editretail = dataGridView1.Rows[oneCell.RowIndex].Cells[12].Value.ToString();

    wartif_UW.editform e2 = new wartif_UW.editform(editloannumber, editloantype, editneworsecond, editpurpose, editstatus, editcomments, editretail,editlid);
    e2.ShowDialog();
 }

这是我的第二个表单构造函数

public editform(int editln, string edittype, string editno2, string purpo, string stat, string come,string reti,int lid)
    {
        InitializeComponent();
        comboload();

        loannumbertxtbox.Text  = editln ;
        loantypecombobox.Text = edittype;
        loanpurposrcombo.Text = purpo;
        neworseccombobox.Text = editno2;
        retailregcombo.Text = reti;
        statuscombbox.Text = stat;
        commnetrichtext.Text = come;
    }

1 个答案:

答案 0 :(得分:0)

DataGridView没有索引器。你必须做这样的事情:

if (dataGridView1.SelectedCells.Count > 0)
        {
            var oneCell = dataGridView1.SelectedCells[0];
            int editloannumber = int.Parse(dataGridView1.Rows[oneCell.RowIndex].Cells[0].Value.ToString());
            int editlid = int.Parse(dataGridView1.Rows[oneCell.RowIndex].Cells[8].Value.ToString());
            string editloantype = dataGridView1.Rows[oneCell.RowIndex].Cells[1].Value.ToString();
            string editneworsecond = dataGridView1.Rows[oneCell.RowIndex].Cells[2].Value.ToString();
            string editpurpose = dataGridView1.Rows[oneCell.RowIndex].Cells[11].Value.ToString();
            string editstatus = dataGridView1.Rows[oneCell.RowIndex].Cells[3].Value.ToString();
            string editcomments = dataGridView1.Rows[oneCell.RowIndex].Cells[7].Value.ToString();
            string editretail = dataGridView1.Rows[oneCell.RowIndex].Cells[12].Value.ToString();

            wartif_UW.editform e2 = new wartif_UW.editform(editloannumber, editloantype, editneworsecond, editpurpose, editstatus, editcomments, editretail, editlid);
            e2.ShowDialog();
        }