Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding checkbox columns to datagrid - windows forms

Status
Not open for further replies.

vinodi

Programmer
May 23, 2003
41
0
0
IN
Hi,

This is the code sample that I am using for displaying records in a datagrid.

sql = "select r.srno 'SNo.',r.gcno 'Gas Connection No.',c.kno 'KNO',r.areacode 'Area Code',c.houseno 'House no.',c.consname 'Name',c.address1 'Address',r.rcptno 'Receipt No.',r.receiptdt 'Receipt Date',r.totrcptamt 'Amount' from tbl_allmastran c,tbl_receipts r where(c.gcno = r.gcno) and r.receiptdt between '" & Format(FromDate.Value, "MM/dd/yyyy") & "' and '" & Format(ToDate.Value, "MM/dd/yyyy") & "'" & _
"union select r.srno 'SNo.',r.gcno 'Gas Connection No.',c.kno 'KNO',r.areacode 'Area Code',c.houseno 'House no.',c.consname 'Name',c.address1 'Address',r.rcptno 'Receipt No.',r.receiptdt 'Receipt Date',r.totrcptamt 'Amount' from tbl_newcon c,tbl_receipts r where(c.gcno = r.gcno) and r.receiptdt between '" & Format(FromDate.Value, "MM/dd/yyyy") & "' and '" & Format(ToDate.Value, "MM/dd/yyyy") & "' order by r.receiptdt,r.gcno,r.rcptno,r.srno"

Dim adapter As New OleDb.OleDbDataAdapter(sql, myconnection)
Dim arcptdataset As New DataSet()
Dim tsl As New DataGridTableStyle()
Dim checkcol As New DataGridBoolColumn()
Try
adapter.Fill(arcptdataset, "receipts")
adapter.Dispose()
Dgriddetails.CaptionText = arcptdataset.Tables("receipts").Rows.Count & " " & "Record(s) found"
Dgriddetails.SetDataBinding(arcptdataset, "receipts")
tsl.MappingName = "receipts"
checkcol.HeaderText = "Checked"
checkcol.MappingName = ""
checkcol.Width = 50
tsl.GridColumnStyles.Add(checkcol)
Dgriddetails.TableStyles.Add(tsl)
Catch
Dgriddetails.CaptionText = "No records present"
End Try

The above code gives me a blank datagrid with the captiontext showing me the number of records.

what should be given in the checkcol.mappingname property, as I want to only create a column of checkboxes.

Can any one out there help me. Thanx in advance. I am using .NET 1.1 (2003).

Regards
Vinodi

~~The only place where SUCCESS comes before WORK is in the dictionary~~
 
Check out the Windows Forms FAQ section about DataGrids, specifically question 5.15:

[URL unfurl="true"]http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q754q[/url]

The code sample is in C#, but it's pretty straightforward. If you have any questions about it, post back here and we'll help out.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Hi jebenson,

I visited the link and made appropriate changes to my code which I am giving below, but still it does not yield me any result. What could be wrong?

Old Code:
Dim adapter As New OleDb.OleDbDataAdapter(sql, myconnection)
Dim arcptdataset As New DataSet()
Dim tsl As New DataGridTableStyle()
Dim checkcol As New DataGridBoolColumn()
Try
adapter.Fill(arcptdataset, "receipts")
adapter.Dispose()
Dgriddetails.CaptionText = arcptdataset.Tables("receipts").Rows.Count & " " & "Record(s) found"
Dgriddetails.SetDataBinding(arcptdataset, "receipts")
tsl.MappingName = "receipts"
checkcol.HeaderText = "Checked"
checkcol.MappingName = ""
checkcol.Width = 50
tsl.GridColumnStyles.Add(checkcol)
Dgriddetails.TableStyles.Add(tsl)
Catch
Dgriddetails.CaptionText = "No records present"
End Try

New Code:
Dim adapter As New OleDb.OleDbDataAdapter(sql, myconnection)
Dim arcptdataset As New DataSet()
Dim tsl As New DataGridTableStyle()
Dim checkcol As New DataGridBoolColumn()
Dim temptable As DataTable
Dim dc As DataColumn
Try
adapter.Fill(arcptdataset, "receipts")
adapter.Dispose()
temptable = New DataTable("temp")
dc = New DataColumn("checkcol")
temptable.Columns.Add(dc)
arcptdataset.Tables.Add(temptable)

Dgriddetails.CaptionText = arcptdataset.Tables("receipts").Rows.Count & " " & "Record(s) found"
Dgriddetails.SetDataBinding(arcptdataset, "receipts")
tsl.MappingName = "receipts"
checkcol.HeaderText = "Checked"
checkcol.MappingName = "checkcol"
checkcol.Width = 50
tsl.GridColumnStyles.Add(checkcol)
Dgriddetails.TableStyles.Add(tsl)
Catch
Dgriddetails.CaptionText = "No records present"
End Try
My problem is to give the end-user the facility of checking the records in a datagrid by putting a mark in a checkbox. If the checkbox is checked that means the user has finished checking the row.

Another option for which you might provide me with a solution is that if I click on a row the entire row gets selected (I have programmed it). What I require is the selection of non-contiguous rows (like if I press CTRL key and click the mouse I can select non contiguous rows in a datagrid). The behavior of selecting non-contigious rows should be incorporated in the click of the mouse itself. That means the user need not keep the CTRL key pressed while clicking the mouse. The combination of CTRL and mouse click should be combined in the mouse click or mouseup events. Is it possible?

Either of the above two solutions would be fine for me, because what I am looking at is a checking mechanism to be incorporated in the datagrid.

Hoping that I am clear in my thoughts and writing them.

Thanx in advance for reading this question of epic proportions and double thanx if any body can provide me with the solution.

Regards,
Vinodi

~~The only place where SUCCESS comes before WORK is in the dictionary~~
 
I used this code to get a chackbox column to appear in a datagrid:

Dim da As SqlDataAdapter
Dim ds As DataSet

da = New SqlDataAdapter("Select * from Issues", conn)
ds = New DataSet
da.Fill(ds, "Issues")

DataGrid2.SetDataBinding(ds, "Issues")

Dim ts As DataGridTableStyle = New DataGridTableStyle

ts.MappingName = "Issues"

Dim boolCol As DataGridColumnStyle = New DataGridBoolColumn

boolCol.MappingName = "CP"
boolCol.HeaderText = "CP"
ts.GridColumnStyles.Add(boolCol)

DataGrid2.TableStyles.Add(ts)

I think the reason you are not seeing anything is the MappingName property of the checkbox column. This property need to be set to the name of a field in the datatable, not the name of the column object.

Also, if you have a boolean field in your datatable (or yes/no in the case of Access), when you bind the data to the datagrid the grid will automatically put in a checkbox column for the boolean field(s), without needing to do the tablestyles code.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
What happens in case I want to have an unbound column of checkboxes which I do not want in any table?

Vinodi

~~The only place where SUCCESS comes before WORK is in the dictionary~~
 
Okay, I found an easier way to do this. You can add a checkbox column by adding a "dummy" column in the SQL select you use to fill the DataSet:

Dim da As SqlDataAdapter
Dim ds As DataSet

da = New SqlDataAdapter("Select [red](cast (0 as bit)) as BoolColumn,[/red] * from Issues", conn)
ds = New DataSet
da.Fill(ds, "Issues")

DataGrid1.DataSource = ds.Tables(0)

The part highlighted in red provides a column that does not actually come from your data, but will show up in the datagrid.

Note that this code

(cast ([red]0[/red] as bit)) as BoolColumn

will make the the checkboxes be unchecked, while this code

(cast ([red]1[/red] as bit)) as BoolColumn

will make the checkboxes be checked.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Hi,

Thanx for your reply. It worked.


Vinodi

~~The only place where SUCCESS comes before WORK is in the dictionary~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top