Results 1 to 9 of 9

Thread: How to add DataColumns by looping through DataGridViewCheckBoxColumn

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    How to add DataColumns by looping through DataGridViewCheckBoxColumn

    I have two forms in my project. They are named "Criteria" and "Screen". The Criteria form contains DataGridViewCriteria. The Screen form contains DataGridViewScreen.

    DataGridViewCriteria's first column is a CheckBoxColumn. DataGridViewCriteria's second column contains a description specific to each row. I need to loop through the CheckBoxColumn and for each CheckBox that is checked I need to add a DataColumn to DataGridViewScreen. I need the title of each DataColumn added to DataGridViewScreen to equal the description that is adjacent its respective CheckBox on DataGridViewCriteria.

    My code so far is:
    Code:
    For Each row As DataGridViewRow In Criteria.DataGridViewCriteria.Rows
            If row.Cells(1).Value = True Then
                Dim col As New DataColumn(row.Cells(1).Value, Type.GetType("System.String"))
                dt.Columns.Add(row.Cells(1).Value)
            End If
        Next
    Any suggestions?

    Thank you very much!

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: How to add DataColumns by looping through DataGridViewCheckBoxColumn

    Why are you adding DataColumn to a datatable if you want to add DataGridViewColumn to a DGV? Apart from that I'm not sure what help you need.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: How to add DataColumns by looping through DataGridViewCheckBoxColumn

    That is the way I have added all of my other columns. Example:
    Code:
    Dim ds As New DataSet
    Dim dt As DataTable
    Dim rankColumn As New DataColumn("Rank", Type.GetType("System.Int32"))
    dt.Columns.Add(rankColumn)
    ds.Tables.Add(dt)
    DataGridViewScreen.DataSource = dt
    Apart from that I need help because the code that I provided in my original post does not throw any errors but no tables are added.

    Thanks for the response!

  4. #4
    Addicted Member
    Join Date
    Sep 2008
    Location
    Reading, UK
    Posts
    192

    Re: How to add DataColumns by looping through DataGridViewCheckBoxColumn

    Hi nordeen1

    What i would do here is have a method in the screen for that you can passin data rows Somthing like

    Code:
    'Add this to the screen form 
    Public Sub Add_Row(ByVal NewRow as DataGridRow)
             DgvScreen.rows.add(NewRow)
    End Sub
    
    
    'then on the critria form 
    For each r as datagridviewrow in dgvcriteria.rows
         If r("What ever your col is called") = true '(tbh not sure thats it works like this)
             Dim nr as New DataGridViewRow 
             'build your new row here make sure
             frmscreen.add_row(nr)
         end if 
    Next


    Ian

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: How to add DataColumns by looping through DataGridViewCheckBoxColumn

    Sorry? That's the way I do it but it doesn't work? Might be an idea to stop doing it that way then!

    So let's be clear about this you have 2 datagridview controls on 2 separate forms.

    Criteria.DataGridViewCriteria is populated with a number of selectable criteria: checkbox/description. So where do these values come from? A database via a datatable, a file?

    First problem is that the description is presumably not a single word so we can't use that as a column heading, so we need a criterion name.

    Checked values in criteria are used to create a new column. Where? A datatable (as you have it at present)? DataGridViewScreen?

    In either case where are the values that will go into these columns coming from? If it's another database table, for example, I wouldn't bother with creating new columns at all when you could simply create a filter or datatview and bind the DGV to that.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: How to add DataColumns by looping through DataGridViewCheckBoxColumn

    12many,

    Thanks for the reply! I've tried your suggestion but I can't seem to get it to work. I want to add columns onto the Screen form instead of rows and I need to be able to select the cell adjacent to each checkbox to label my columns with.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: How to add DataColumns by looping through DataGridViewCheckBoxColumn

    dunfiddlin,

    I obviously know that the way I am doing it right now doesn't work. That is why I can here for advice.

    Yes, I have 2 datagridview controls on 2 separate forms.

    Criteria.DataGridViewCriteria is populated with a number of selectable criteria: checkbox/description. I manually created a datatable and filled it with data and then I bound it to DataGridViewCriteria.

    In regards to creating a criterion name. I don't know much about that and may need more details.

    The checked values in Criteria will be used to create a new column in a datatable that is bound to DataGridViewScreen.

    The values that will go into these columns will be coming from the internet.

    I hope this helps to make my situation more clear. Any suggestions are appreciated! If there are any ways in which I can make my project more efficient I would love to know!

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2013
    Posts
    7

    Re: How to add DataColumns by looping through DataGridViewCheckBoxColumn

    Could anyone tell me how to set the checked event of the CheckBoxColumn's cells?

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: How to add DataColumns by looping through DataGridViewCheckBoxColumn

    Ok, first thing to say is that checkboxes in the DatatGridView are an absolute nightmare to work with so I would advise setting up the Criteria DGV columns at design time rather than inheriting them from the datatable. You also need to add a second column containing a one word title for each criterion which can be used as the column header in the second DGV. So in the example below I have dgvCriteria with columns Selector (CheckBox Column), Criterion (Text), Description (Text).

    In the Selector column, you must set the TrueValue property (I've used a simple T) and remember that this is a string value. So if you set it to True, for example, you would compare it to "True" not the usual True constant.

    It is one of the weirdnesses of the DGV checkbox column, however, that you set values using the Boolean True/False, despite the fact that you get values using the custom setting of the TrueValue and FalseValue properties. You must ensure that all the rows have a value in the checkbox column so remember to disable user add rows. eg.
    vb.net Code:
    1. dgvCriteria.Rows(0).Cells(0).Value = True

    So, at last to the code ...

    vb.net Code:
    1. Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2.         Dim dt As New DataTable ' done her so that if there is another run it will not add to the existing table
    3.         For Each dr As DataGridViewRow In dgvCriteria.Rows
    4.             If CType(dr, DataGridViewRow).Cells(0).Value.Equals("T") Then
    5.                 dt.Columns.Add(New DataColumn(CType(dr, DataGridViewRow).Cells(1).Value.ToString))
    6.             End If
    7.         Next
    8.         DataGridView2.DataSource = dt
    9.     End Sub
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width