Subscribe to Subscribers
Talk to Webmaster Tony John


Resources » Code Snippets » Winforms DataGrid

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


Posted Date:     Category: Winforms DataGrid    
Author: Member Level: Diamond    Points: 30


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)





  • Did you like this resource? Share it with your friends and show your love!


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

    No responses found. Be the first to respond...

    Feedbacks      

    Post Comment:




  • 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:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to show link button datagridview windows application ?
    Previous Resource: AutoComplete in DataGridView using Win Forms
    Return to Resources
    Post New Resource
    Category: Winforms DataGrid


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Datagridviw checkbox row display  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.