Click here to Skip to main content
16,016,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a table Department (DeptID,DeptName,Location) in database.

Need to get data into datagridview where the first column must be a Checkbox and last column must be a delete button.

1) How to do this...I am not getting any idea as I have never tried in Windows app.
2) How to select a row when checkbox is selected and Delete button is clicked.

Thank you..

Prathap
Posted
Comments
[no name] 25-Dec-12 1:16am    
Prathap Gangireddy 25-Dec-12 1:51am    
Hi, Thanks for the reply. Below is my code. SqlConnection cn=new SqlConnection("server=PRATHAPG\\SQLEXPRESS;database=Prathap;trusted_connection=true"); SqlCommand cmd=new SqlCommand("Select * from Depts",cn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable ds = new DataTable(); da.Fill(ds); DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn(); gvdept.Columns.Add(chk); chk.HeaderText = "Select"; chk.Name = "chk"; gvdept.DataSource = ds; But I am not able to select the checkbox.Do i need to make some changes in the selectionmode and How to delete the row when we select the checkbox.
Prathap Gangireddy 25-Dec-12 2:41am    
Hi Rohit,

I have sorted out the problem.Now everything works fine.Can you please help me how to delete a particular row when checkbox is checked.

Thanks.
Prathap.

The DataGridView control and its related classes are designed to be a flexible, extensible system for displaying and editing tabular data. The DataGridView control provides TextBox, CheckBox, Image, Button, ComboBox and Link columns with the corresponding cell types. While adding a CheckBox in a cell, the cell Value property can be set explicitly through programmatic code that accesses the cell in the Cells collection of the row, or it can be set through data binding.

The following C# code shows how to add a CheckBox in Cell of a DataGridView control and set the third row checkbox value as true.
C#
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
dataGridView1.Columns.Add(chk);
chk.HeaderText = "Check Data";
chk.Name = "chk";
dataGridView1.Rows[2].Cells[3].Value = true;


And this shows how to add a button;
C#
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
dataGridView1.Columns.Add(btn);
btn.HeaderText = "Click Data";
btn.Text = "Click Here";
btn.Name = "btn";
btn.UseColumnTextForButtonValue = true;


Good luck,
OI
 
Share this answer
 
Comments
Prathap Gangireddy 25-Dec-12 1:51am    
Hi, Thanks for the reply.

Below is my code.

SqlConnection cn=new SqlConnection("server=PRATHAPG\\SQLEXPRESS;database=Prathap;trusted_connection=true");
SqlCommand cmd=new SqlCommand("Select * from Depts",cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable ds = new DataTable();
da.Fill(ds);
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
gvdept.Columns.Add(chk);
chk.HeaderText = "Select";
chk.Name = "chk";
gvdept.DataSource = ds;





But I am not able to select the checkbox.Do i need to make some changes in the selectionmode and How to delete the row when we select the checkbox.
 
Share this answer
 
Comments
Prathap Gangireddy 25-Dec-12 2:41am    
Hi,

I have sorted out the problem.Now everything works fine.Can you please help me how to delete a particular row when checkbox is checked.

Thanks.
Prathap.
ridoy 25-Dec-12 2:45am    
check..
http://stackoverflow.com/questions/6439667/how-to-remove-rows-in-data-grid-view-where-checkbox-is-checked
http://social.msdn.microsoft.com/Forums/eu/csharplanguage/thread/90334fcb-adec-4e9a-9148-763eed1fbb33
http://www.codeproject.com/Questions/458516/Can-not-Delete-Checked-Rows-in-DatagridView
Prathap Gangireddy 25-Dec-12 2:59am    
Hi Ridoy,

Thanks for the links.But most of the solutions include iterating through all the rows and I want to delete only the selected row.

Is it possible.
Prathap Gangireddy 25-Dec-12 3:03am    
hi Ridoy,

Thanks for the help.

Below are my mistakes

1) Not able to check the checkbox - Datagrid readonly property was true.So changed to false.

2)Not able to add Checkbox at the first column and Delete button in lastrow.Then did changed to code..working fine

SqlConnection cn=new SqlConnection("server=PRATHAPG\\SQLEXPRESS;database=Prathap;trusted_connection=true");
SqlCommand cmd=new SqlCommand("Select * from Depts",cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(ds);
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
gvdept.Columns.Add(chk);
chk.HeaderText = "Select";
chk.Name = "chk";
gvdept.AllowUserToAddRows = false;
gvdept.DataSource = ds;
gvdept.Columns[1].Visible = false;
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
btn.Name = "btnDeleterow";
btn.HeaderText = "Delete";
gvdept.Columns.Add(btn);

3) Not able to delete when checkbox is checked.Now finally solved it

private void gvdept_CellClick(object sender, DataGridViewCellEventArgs e)
{
int i = e.RowIndex;
if ((bool)gvdept.Rows[i].Cells[0].FormattedValue)
{

if (e.ColumnIndex >= 0)
{
if (gvdept.Columns[e.ColumnIndex].Name == "btnDeleterow")
{
// gvdept.Rows.RemoveAt(e.RowIndex);
string deptid = gvdept.Rows[e.RowIndex].Cells[2].Value.ToString();
MessageBox.Show(deptid);

}
}
}
ridoy 25-Dec-12 3:05am    
glad to see you solve those..happy coding..:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900