如何在文本框C#中显示DataGrid行值?

时间:2019-02-19 10:02:29

标签: c# .net winforms

我正在开发应用程序。我需要在文本框中显示行值的地方。有时这有效,但有时却无效。请帮忙。这是我正在使用的代码:

private void CRUD_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        txtBoxID.Text = CRUD.Rows[e.RowIndex].Cells[0].Value.ToString();
        txtBoxStates.Text = CRUD.Rows[e.RowIndex].Cells[1].Value.ToString();
        txtBoxName.Text = CRUD.Rows[e.RowIndex].Cells[2].Value.ToString();
        txtBoxAddress.Text = CRUD.Rows[e.RowIndex].Cells[3].Value.ToString();
        txtBoxCenter.Text = CRUD.Rows[e.RowIndex].Cells[4].Value.ToString();
        txtBoxCity.Text = CRUD.Rows[e.RowIndex].Cells[5].Value.ToString();


        DataRow Row = tblCRUD.Rows[e.RowIndex];
        DataRow newRow = tblCRUD.NewRow();
        newRow.ItemArray = Row.ItemArray;
        tblCRUD.Rows.Remove(Row);
        tblCRUD.Rows.InsertAt(newRow, 0);
        foreach (DataGridViewRow item in CRUD.Rows)
        {
            CRUD.Rows[item.Index].Selected = false;
        }
        CRUD.Rows[0].Selected = true;
    }

1 个答案:

答案 0 :(得分:0)

我想这就是你想要的:

void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridViewCell dataGridViewCell = null;

        if (e.ColumnIndex != -1 && e.RowIndex != -1)
        {
            DataGridView dataGridView = sender as DataGridView;
            if (dataGridView != null)
                dataGridViewCell = dataGridView[e.ColumnIndex, e.RowIndex];
        }
        else
            dataGridViewCell = null;

        if (dataGridViewCell != null)
            textBox1.Text = dataGridViewCell.Value.ToString();
    }