Dan
Dan

Reputation: 883

Get dataGridView selected row value

I have the following class and I want to get the current selected row value and set it to a textbox:

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

I used richTextBox1.Text = dataGridView.CurrentCell.RowIndex.ToString(); to get wineName but it returns 0;

Here's an image: enter image description here

Upvotes: 1

Views: 16110

Answers (2)

jong velasquez
jong velasquez

Reputation: 31

this is work for me:

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

Upvotes: 2

Arsalan Qaiser
Arsalan Qaiser

Reputation: 457

try this

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

Upvotes: 1

Related Questions