How to show selected checkbox row from datagridview to textbox in C#.NET?


In this article I am going to explain about to display selected checkbox datagridview row details in to textbox. This technique I get all details of cell and display in textbox.

Description


In this technique i have applied user able to select only one checkbox at the time, then only show the selected record detail I in the textbox.

Form design


Images

Code Behind



using System.Data;
namespace SelChkRowVal
{
public partial class Form1 : Form
{
DataTable dt = new DataTable();
DataRow dr;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
loadGrid();
}

public void loadGrid()
{
dataGridView1.AllowUserToAddRows = false;

//Below i create on check box column in the datagrid view
dataGridView1.Columns.Clear();
DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
//set name for the check box column
colCB.Name = "chkcol";
colCB.HeaderText = "";

dataGridView1.Columns.Add(colCB);

DataTable dt = new DataTable();
DataRow dr = default(DataRow);

//Declare Column names
dt.Columns.Add("eno");
dt.Columns.Add("empname");
dt.Columns.Add("sal");

//Create rows with data
dr = dt.NewRow();
dr["eno"] = 101;
dr["empname"] = "test1";
dr["sal"] = 9000;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["eno"] = 102;
dr["empname"] = "test2";
dr["sal"] = 15000;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["eno"] = 103;
dr["empname"] = "test3";
dr["sal"] = 20000;
dt.Rows.Add(dr);

dataGridView1.DataSource = dt;
}
//Below method is used check only one checkbox in grid view
private void dataGridView1_CellClick(System.Object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["chkcol"].Value) == false)
{
for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
dataGridView1.Rows[i].Cells["chkcol"].Value = false;
}
}
}
}

private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text="";

int i = 0;
List ChkedRow = new List();

for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["chkcol"].Value) == true)
{
ChkedRow.Add(i);
}
}

if (ChkedRow.Count == 0)
{
MessageBox.Show("Select atleast one checkbox");
return;
}

//Here i display all selected checkbox data in that three text boxes
foreach (int k in ChkedRow)
{
textBox1.Text = textBox1.Text + dataGridView1.Rows[k].Cells[1].Value + " ";
textBox2.Text = textBox2.Text + dataGridView1.Rows[k].Cells[2].Value + " ";
textBox3.Text = textBox3.Text + dataGridView1.Rows[k].Cells[3].Value + " ";
}
}
}
}

Output


Images

Source code:

Client Side: Form design
Code Behind: C#

Conclusion

I hope this article is help you to know about show selected checkbox row details.


Attachments

  • Source _code (44022-0470-Source-code.rar)
  • Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: