hey guys how i display datagridview cell values in a listbox once i click on it
heres what i have so far but it doesnt work

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

          listBox1.Text = dataGridView1.Rows[e.RowIndex].Cells["Description"].Value.ToString();

        }

please help thanks in advanced

Dani AI

Generated

Short answer: add the cell's displayed string to the ListBox's items (or bind the ListBox to the same data source) and guard against invalid rows and null/DBNULL values. pointed in the right direction about adding an item, and is correct that the ListBox's Text property is not the way to place new entries into the list.

A robust CellDoubleClick handler (checks and uses the cell's formatted text) — this avoids header clicks, nulls, and DBNull values:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex < 0) return; // header or invalid row
    var cell = dataGridView1[e.ColumnIndex, e.RowIndex];
    var text = (cell?.FormattedValue ?? string.Empty).ToString().Trim();
    if (string.IsNullOrEmpty(text)) return;
    listBox1.Items.Add(text);
    listBox1.SelectedIndex = listBox1.Items.Count - 1;
}

If the grid is bound (DataTable / BindingSource) prefer data binding instead of manual adds — keeps the UI in sync and avoids duplicates:

// reuse the same source as the grid
listBox1.DisplayMember = "Description";
listBox1.ValueMember = "ID";      // optional
listBox1.DataSource = myBindingSourceOrDataTable;

Quick checklist / troubleshooting:

  • Ensure the event is actually wired (designer or += handler).
  • Protect against e.RowIndex == -1 and null/DBNull cell values.
  • Use FormattedValue when you want the text as shown in the grid (formatting applied).
  • If adding many items, wrap updates with BeginUpdate/EndUpdate for performance.
  • To copy a whole row, loop the row's Cells and build a single string before adding.

These steps expand on the suggestions from and with the safety and binding options you’ll need for production use.

Recommended Answers

All 2 Replies

try this

listBox1.Items.Add( dataGridView1.Rows[e.RowIndex].Cells["Description"].Value.ToString());

The Listbox.Text property is used to Search for specific text. So what you are actually doing is searching the listbox for dataGridView1.Rows[e.RowIndex].Cells["Description"].Value.ToString(); the method george suggested will work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.