How To Get Data From Datagridview Row In The Text Boxes RRS feed

  • Question

  • I had added a Columns to a dataGridView Like This  

    dataGridView1.Columns.Add("mc", "Manual Columns");

    The dataGridview output like this 

    From MySQL             From MySQL       Manual Columns
    Visual       Studio     c# (Manual input)

    i had success use  following code get " label1.Text is  VusualStudio ". How can i get Manual Columns value c#? 

    Many Thank !!~

    string valuetest = row.Cells[0].Value.ToString();
    string valuetest1 = row.Cells[1].Value.ToString();
    label1.Text = valuetest + valuetest1 ;

    • Edited by takshing2001 Monday, June 17, 2019 8:45 AM
    • Moved by CoolDadTx Monday, June 17, 2019 1:43 PM Winforms related
    Monday, June 17, 2019 8:40 AM

All replies

  • Hello,

    The following will concatenate all cells on a row into a string and does null checks for cell values. Note the indexer rows[0] for doing the first row.

    var cellData = string.Join(" ", dataGridView1.Rows[0].Cells.Cast<DataGridViewCell>()
        .Select(cell => ((cell.Value == null) ? "" : cell.Value.ToString())));
    label1.Text = cellData;

    To go deeper we can even do all rows to delimited strings but that is more than you are looking for.

    var result = dataGridView1.Rows.Cast<DataGridViewRow>()
        .Where(row => !row.IsNewRow)
        .Select(row => new {
            RowItem = string.Join(",", Array.ConvertAll(row
                cell => ((cell.Value == null) ? "" : cell.Value.ToString())))
        }).Select(item => item.RowItem).ToList();

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Monday, June 17, 2019 10:07 AM
  • Hi,

    Since the value of "Manual Columns" is manually entered, you need to check if it is "Null".




    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, June 18, 2019 2:20 AM