Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2004

Tip: Looking for answers? Try searching our database.

Loop through datagrid to get values of boolean columns

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rekha - 07 Dec 2004 17:51 GMT
Any pointers as to how to loop through a winforms datagrid to get all the
selected(checked) columns?
blameMike - 07 Dec 2004 19:51 GMT
> 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.
Rekha - 10 Dec 2004 15:19 GMT
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

> > Any pointers as to how to loop through a winforms datagrid to get all the
> > selected(checked) columns?
[quoted text clipped - 10 lines]
>
> I hope this helps.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.