Home All Groups Group Topic Archive Search About

Loop through datagrid to get values of boolean columns

Author
7 Dec 2004 5:51 PM
Rekha
Any pointers as to how to loop through a winforms datagrid to get all the
selected(checked) columns?

Author
7 Dec 2004 7:51 PM
blameMike
"Rekha" wrote:

> Any pointers as to how to loop through a winforms datagrid to get all the
> selected(checked) columns?

One approach would be to loop through the DataSource of the grid, as in:

foreach (DataRow row in ((DataTable)dataGrid1.DataSource).Rows)
{
    MessageBox.Show(row["OK"].ToString());
}

When the value of the grid cells are changed, the underlying datasource is
changed as well.

I hope this helps.
Are all your drivers up to date? click for free checkup

Author
10 Dec 2004 3:19 PM
Rekha
Hi Mike

the boolean column (checkbox column) in a datagrid, how to get the values of
the checkbox column(check/uncheck).
depending on check box column i need to process the row.

sample code

Private Function PopulateGridWithSearchResults(ByVal StrSql As String) As
Boolean
        Try
            Dim strConn As String
            Dim numRecords As Integer

            strConn = "Initial Catalog=;Data Source=;Integrated Security=;"
           Dim sqlClientConnection As New SqlConnection(strConn)
            Dim eblsDotNetDataAdapter As New SqlDataAdapter()
            DataAdapter.SelectCommand = New SqlCommand(StrSql,
sqlClientConnection)
            Dim myDS As DataSet = New DataSet()

            DataAdapter.Fill(myDS, "MyTable")
            numRecords = myDS.Tables(0).Rows.Count()
            If numRecords > 0 Then
                dgCases.CaptionText = "Found : " & numRecords & " matching
records."
                FormSizeReset(575, 500)

                Dim ts As New DataGridTableStyle()
                ts.MappingName = myDS.Tables(0).TableName
                Dim dc As New DataColumn("MyBoolColumn", GetType(Boolean))
                dc.DefaultValue = False
                myDS.Tables("MyTable").Columns.Add(dc)

                Dim txtID6 As New DataGridBoolColumn()
                txtID6.MappingName = "MyBoolColumn"
                txtID6.HeaderText = "CHECKBOX"
                txtID6.AllowNull = False
                txtID6.Width = 80
                ts.GridColumnStyles.Add(txtID6)


                Dim txtID0 As New DataGridTextBoxColumn()
                txtID0.MappingName = "caseid"
                txtID0.HeaderText = "caseid"
                txtID0.Width = 80
                ts.GridColumnStyles.Add(txtID0)

                Dim txtID As New DataGridTextBoxColumn()
                txtID.MappingName = "MemberID"
                txtID.HeaderText = "MemberID"
                txtID.Width = 50
                ts.GridColumnStyles.Add(txtID)

                Dim txtID1 As New DataGridTextBoxColumn()
                txtID1.MappingName = "Plan"
                txtID1.HeaderText = "Plan"
                txtID1.Width = 80
                ts.GridColumnStyles.Add(txtID1)
                dgCases.SetDataBinding(myDS, "MyTable")
                dgCases.TableStyles.Clear()     'clear the contents for
second try.
                dgCases.TableStyles.Add(ts)
                dgCases.SetDataBinding(myDS, "MyTable")
                dgCases.TableStyles(0).GridColumnStyles(1).Width = 0
end function

'function to get the bool value
Private Sub DataGrid1_MouseUp(ByVal sender As Object, ByVal e As
MouseEventArgs) Handles dgCases.MouseUp

        Dim hti As DataGrid.HitTestInfo = Me.dgCases.HitTest(e.X, e.Y)
        If hti.Column = 0 Then
            dgCases.Item(hti.Row, hti.Column) = Not
CBool(dgCases.Item(hti.Row, hti.Column))
        End If
end sub

Private Sub button_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles button.Click

Dim s As String = "Selected rows:"
        Dim o As Object
        For Each o In GetSelectedRows(dgCases)
            s += " " + o.ToString()
        Next o
        MessageBox.Show(s)
end sub

Public Function GetSelectedRows(ByVal dgCases As DataGrid) As
System.Collections.ArrayList
        Dim al As New ArrayList()
        Dim cm As CurrencyManager = Me.BindingContext(dgCases.DataSource,
dgCases.DataMember)
        Dim dv As DataView = CType(cm.List, DataView)
        Dim i As Integer
        For i = 0 To dv.Count - 1

? i need to get the checked column  alone how can i do it using the if
statement

            If  ? ??????  Then
                al.Add(i)
            End If
        Next
        Return al
    End Function 'GetSelectedRows
thanks
rekha

Show quoteHide quote
"blameMike" wrote:

>
>
> "Rekha" wrote:
>
> > Any pointers as to how to loop through a winforms datagrid to get all the
> > selected(checked) columns?
>
> One approach would be to loop through the DataSource of the grid, as in:
>
>  foreach (DataRow row in ((DataTable)dataGrid1.DataSource).Rows)
> {
>     MessageBox.Show(row["OK"].ToString());
> }
>
> When the value of the grid cells are changed, the underlying datasource is
> changed as well.
>
> I hope this helps.
>
>

Bookmark and Share