• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

adding a column to a bound datagrid

I have spent awhile looking on the internet to figure out how to add a column to a datagrid on a windows form in vb.net.  I keep running into this thing called a template column but i can't find anything to explain where this asp code goes I think maybe this is relating to webforms and not Windows Forms. So possibly i am looking for the wrong thing.  Can someone help me out.
0
ethnarch
Asked:
ethnarch
  • 9
  • 9
1 Solution
 
RonaldBiemansCommented:
This column is it an unbound column,  otherwise you can just add it to the underlaying datasource
0
 
RonaldBiemansCommented:
so if your datasource for your grid would be something like

datagrid.datasource = ds.tables(0)

you could add a column  like

ds.tables(0).columns.add("yourextracolumn")
datagrid.datasource = ds.tables(0)
0
 
RonaldBiemansCommented:
What do you want to do with the extra column ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ethnarchAuthor Commented:
thankyou that was ridiculously simple

i actually want to add a checkbox column

i have one more question though when i go to update the actual database table from the dataset it is not going to write the new columns correct?  Or maybe what i should ask is there isn't anything special i have to do to prevent this from happening is there?
0
 
RonaldBiemansCommented:
no it shouldn't (because they don't exist it the original table).
0
 
ethnarchAuthor Commented:
So how do i get the added column to become a checkbox or combobox?
0
 
RonaldBiemansCommented:
look at this

http://64.78.52.104/FAQ/WinForms/FAQ_c44c.asp#q480q

look at article 5.9

It explain (with code) how to do this
0
 
ethnarchAuthor Commented:
Ok the code you gave me on that site was for some other programming language.  I did manage to convert it as best i could and now i keep getting an error.

An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

Additional information: Can't create a child list for field yourextracolumn.
---------
Below is the Source code i have so far.

  Private Sub WorkModule_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        OleDbDataAdapter1.Fill(Cids1)
        Cids1.Tables(0).Columns.Add("yourextracolumn")
        DataGrid1.DataSource = Cids1.Tables(0)
        AddCustomDataTableStyle()
        'Cids1.Tables("changdItems").Columns.Add("hello")
        'checkCol.HeaderText = "Approved";
        'checkCol.DataField = "Approved";

    End Sub
    Sub AddCustomDataTableStyle()

        'STEP 1: Create a DataTable style object and set properties if required.
        Dim ts1 = New DataGridTableStyle()

        'specify the table from dataset (required step)
        ts1.MappingName = "changedItems"

        ' Set other properties (optional step)
        ts1.AlternatingBackColor = Color.LightBlue

        'STEP 2: Create a string column and add it to the tablestyle
        Dim TextCol = New DataGridTextBoxColumn()
        TextCol.MappingName = "custName"
        TextCol.HeaderText = "Customer Name"
        TextCol.Width = 100
        ts1.GridColumnStyles.Add(TextCol)

        'STEP 3: Create an int column style and add it to the tablestyle
        'this requires setting the format for the column through its property descriptor
        Dim pdc = Me.BindingContext(Cids1, "yourextracolumn").GetItemProperties()
        'now created a formated column using the pdc
        Dim csIDInt = New DataGridTextBoxColumn(pdc("yourextracolumn"), "i", True)
        csIDInt.MappingName = "yourextracolumn"
        csIDInt.HeaderText = "yourextracolumn"
        csIDInt.Width = 50
        ts1.GridColumnStyles.Add(csIDInt)

        'STEP 4: Add the checkbox
        Dim boolCol = New DataGridBoolColumn()
        boolCol.MappingName = "Current"
        boolCol.HeaderText = "Info Current"
        boolCol.Width = 70
        ts1.GridColumnStyles.Add(boolCol)


        'STEP 5: Add the tablestyle to your datagrid’s tablestlye collection
        DataGrid1.TableStyles.Add(ts1)

    End Sub


End Class
0
 
RonaldBiemansCommented:
The link I gave you has both downloadable samples in both VB and C#
0
 
RonaldBiemansCommented:
Where do you get the error (on what line)
0
 
ethnarchAuthor Commented:
I got the error on this line

Dim pdc = Me.BindingContext(Cids1, "yourextracolumn").GetItemProperties()
0
 
ethnarchAuthor Commented:
Let me just re-hash my question so it's more clear what i am trying to accomplish

I have a datagrid, which is bound to a dataset that is filled with data from a database.

I want to be able to add columns to the datagrid that have checkboxes in them so that i can use the information from the check boxes to determine what action should be taken on the data.

I am sorry about the confusion but i really wanted the checkbox more then the combobox example so i found the other example 5.15 on the page  and that one, only had the c# example so i did my best to convert it.  I am not an expert in either of the languages so the problem could just be a mistake in my syntax.
0
 
RonaldBiemansCommented:
ok try this instead

Cids1.Tables(0).Columns.Add("yourextracolumn",gettype(system.boolean))

or

Cids1.Tables(0).Columns.Add("yourextracolumn",gettype(system.integer))

I'm going home now but I'll be back tomorrow morning and answer it, if the above doesn't work.
0
 
ethnarchAuthor Commented:
That worked but now i can't seem to set the value of the column i am trying to set the default value to False like this

        Dim i As Integer

        OleDbDataAdapter1.Fill(Cids1)
        Cids1.Tables(0).Columns.Add("UseLocationB", GetType(System.Boolean))

        For i = 0 To Cids1.Tables(0).Rows.Count - 1
            Cids1.Tables(0).Rows.Item("UseLocationB") = 1
        Next
        'Dim ts1
0
 
ethnarchAuthor Commented:
oh sorry that's not the code i was using this is

        Dim i As Integer

        OleDbDataAdapter1.Fill(Cids1)
        Cids1.Tables(0).Columns.Add("UseLocationB", GetType(System.Boolean))

        For i = 0 To Cids1.Tables(0).Rows.Count - 1
            Cids1.Tables(0).Rows.Item("UseLocationB") = False
        Next
        'Dim ts1
0
 
ethnarchAuthor Commented:
and it tells me that it's read only
0
 
RonaldBiemansCommented:
you are not using the loop there,  are you. It should be

 For i as integer = 0 To Cids1.Tables(0).Rows.Count - 1
       Cids1.Tables(0).Rows(i).Item("UseLocationB") = False
 Next
0
 
ethnarchAuthor Commented:
thanks i feel really stupid now i can't believe i missed that.

thankyou for all the help
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now