Question : difficulty in binding DataGridView in windows form

Hello Experts,

I am facing one difficulty in binding DataGridView in windows form. DataGridView should have three columns EmpName, DeptName, IsOnPayroll. I want to display DeptName in drop Down which should be editable directly and IsOnPayroll is a bit field, it should be displayed in check box and editable too. How can I do it? Herewith I am giving table creation script.

 


create table empDept
(
      deptId int identity(1,1),
      deptName varchar(10)
)
go

insert into empDept
select 'MIS' union all
select 'Account' union all
select 'QA'
GO

create table employeeData
(
      empId int identity(1,1),
      empName varchar(20),
      isOnPayroll bit,
      deptnId int
)
go

insert into employeeData
select 'ritesh shah',0,1 union all
select 'rajan jain',1,2 union all
select 'alka shah',1,3
GO

select e.empId,e.empName,d.deptName,e.isOnPayroll from employeeData as e join empDept as d on e.deptnId=d.deptId



I have actually worked very less in windows form so facing this problem. Any help from anybody would be highly appreciated.

Answer : difficulty in binding DataGridView in windows form

You need to handle the CellEndEdit event. This is where you can get the changed value and save the modified value to your database.
1:
2:
3:
4:
5:
6:
7:
8:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        string changedValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        // go ahead with saving the new value here
    }
}
Random Solutions  
 
programming4us programming4us