获取dataGridView选中的行值

时间:2014-04-14 21:12:17

标签: c# visual-studio

我有以下类,我想获取当前选定的行值并将其设置为文本框:

private void UpdateWines() {
            string evalStr = "(WINES::get-wine-list)";
            MultifieldValue mv = (MultifieldValue)enviroment.Eval(evalStr);
            List<WineRecommendation> wineRecList = new List<WineRecommendation>();

            for (int i = 0; i < mv.Count; i++) {
                FactAddressValue fv = (FactAddressValue)mv[i];
                int certainty = (int)((FloatValue)fv.GetFactSlot("certainty"));
                String wineName = ((StringValue)fv.GetFactSlot("value"));
                wineRecList.Add(new WineRecommendation() { WineName = wineName, Certainty = certainty });
            }

            dataGridView.DataSource = wineRecList;
            //richTextBox1.Text = dataGridView1.SelectedCells[0].Value;
            richTextBox1.Text = dataGridView.CurrentCell.RowIndex.ToString();
        }

我使用richTextBox1.Text = dataGridView.CurrentCell.RowIndex.ToString();来获取 wineName ,但它返回0;

这是一张图片:enter image description here

2 个答案:

答案 0 :(得分:2)

这对我有用:

private void dgvSubject_CellClick(object sender, DataGridViewCellEventArgs e)
if (e.RowIndex >= 0)
{
    DataGridViewRow row = this.dgvSubject.Rows[e.RowIndex];

    txtSubjectCode.Text = row.Cells["isid"].Value.ToString();
}

- jongvelasquez

答案 1 :(得分:1)

试试这个

 richTextBox1.Text=dataGridView1.Rows[e.RowIndex].Cells["Your Coloumn name"].Value.ToString();