Jack Sparrow
Jack Sparrow

Reputation: 5

DataGridView In C# to select data and show it to one textbox

I am working on a windows form application, I have made datagridview for showing my data but I also want to select some specific columns from Datagridview and show them to only one TextBox.

I have tried but it is only getting last Column named "SEIKEN_NO" to textbox but I want multiple column values to show in One TextBox.

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        // to set oem no, fic_no and seiken_no to textbox named particular1Txt.

        dataGridView1.Refresh();

        try
        {
            if (RB1.Checked == true)
            {
                int i;
                i = dataGridView1.SelectedCells[0].RowIndex;

                Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();
                Particular1Txt.Text = dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString();
                Particular1Txt.Text = dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
            }
            else if (RB2.Checked == true)
            {
                int i;
                i = dataGridView1.SelectedCells[0].RowIndex;
                Particular2Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();
                Particular2Txt.Text = dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString();
                Particular2Txt.Text = dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
            }

Upvotes: 0

Views: 1506

Answers (2)

vijayvicks
vijayvicks

Reputation: 163

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    // to set oem no, fic_no and seiken_no to textbox named particular1Txt.
    dataGridView1.Refresh();
    try
    {
        if (RB1.Checked == true)
        {
            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;
            Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString()+" "+ dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString()+" "+dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
        }
        else if (RB2.Checked == true)
        {
            int i;
            i = dataGridView1.SelectedCells[0].RowIndex;
            Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString()+" "+ dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString()+" "+dataGridView1.Rows[i].Cells["Seiken_NO"].Value.ToString();
        }
    }
}

Upvotes: 1

Ferus7
Ferus7

Reputation: 727

Fore each line you assign data to the TextBox:

Particular1Txt.Text = dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();

change to:

Particular1Txt.Text += dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString();

note the +=

otherwise you are overwriting the content of the TextBox

For values concatenation formatting, you can:

+= " " + dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString(); // very basic way to do it

Another way example with String.Format()

Particular1Txt.Text=String.Format({0} {1} {2}), dataGridView1.Rows[i].Cells["FIC_No"].Value.ToString(),dataGridView1.Rows[i].Cells["OEM_No"].Value.ToString(),dataGridView1.Rows[i].Cells["Seiken_No"].Value.ToString();

You can also use: String.Join() with an string array, or the StringBuilder class,

hope this help you

Upvotes: 2

Related Questions