Adding columns to DataGrid

Hello All,

I am trying to add columns to the DataGrid and ultimately my database from the Button_Click event and am at wits end.

I tried the code from the msdn library to start with and got the squigglies from DATATABLE in their code.After playing with the code for a little while and got rid af the squigglies and ran it , I got the error message : Object reference not set to an instance of an object.

I have also tried code from my books and have had no luck.This is the last block that I have tried:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim mytable As DataSet1.HammersDataTable

Dim myColumn As DataColumn

myColumn = New DataColumn

With myColumn

.ColumnName = "SupplierID"

.DataType = System.Type.GetType("System.String")

.Unique = True

.AutoIncrement = False

.Caption = "SupplierID"

.ReadOnly = False

End With

' Add the column to the table's columns collection.

mytable.Columns.Add(myColumn)

End Sub

I get the same error from this as well.

Am I missing something here



Answer this question

Adding columns to DataGrid

  • buster2001

    the version shouldnt matter. Are you sure you changed the word "index" to a number The index was just to indicate that an index goes there, normally to get the first index (datatable) it would be 0



  • Sajitha Jose

    Hmmmm....Hello Davco

    >Am I missing something here

    If I understand your requirement, then yes you are missing something.

    If I understand you correctly, you want to add columns to a database table, not rows. If this is so, then I'll first warn you that you should never allow users to modify the structure of your databases. Your database structure should already reflect the data requirements of your application before you even begin your application design. Furthure more, to accomplish what you want to do will involve close coupling of your database access code to your application. This also adds to a weak design for your application. That being said, I'll give you some advice on how to commit these programming sins!

    You do not want to alter the datagrid and then have the datatable reflect that alteration. It's the other way around, you have to alter the datatable then have the datagrid reflect the new state of the datatable structure

    You have to make this change using Data Definition Language (DDL) SQL using the SQLClient Command Object. The SQL would use the "Alter Table" syntax.

    Once the table has been altered with the added column, a repolulation of the dataset should now contain the table with the added column(s)

    Ibrahim (Don't Do It) Malluf


  • byronfromwesleyan

    Hello Davco

    OK; have at it

    Below is some code that will add a column to a table from VB.net. The SQL string is very basic alter table command. I suggest that you look up Alter Table in sql-server books online or visit google university for more detail. There is a lot more you can do than just simply add a column.

    Enjoy

    Ibrahim (If you got to then you got to) Malluf

    Private Sub AddColumn()

    Dim MyConn As SqlClient.SqlConnection

    Dim MyCMD As SqlClient.SqlCommand

    Dim MySQL As String

    Try

    'replace 'Test' with your table name, 'test3' with your column name.

    'replace varchar(50) with your data type.

    MySQL = "ALTER TABLE Test ADD Test3 varchar(50)"

    MyConn = New SqlClient.SqlConnection("YOUR CONNECTION STRING")

    MyConn.Open()

    MyCMD = New SqlClient.SqlCommand(MySQL, MyConn)

    MyCMD.ExecuteNonQuery()

    Catch sql_ex As SqlClient.SqlException

    MsgBox(sql_ex.Message)

    Catch ex As Exception

    MsgBox(ex.Message)

    Finally

    If MyConn IsNot Nothing And MyConn.State <> ConnectionState.Closed Then

    MyConn.Close()

    MyConn.Dispose()

    End If

    End Try

    End Sub


  • J Russo

    Ibrahim,

    Thanks for understanding what I want to do.

    I am a beginner and it seems that I only want to do what is uncommon ,but I need to understand all of the concepts of programming , and if I don`t understand all , then I don`t have a full understanding.

    I know that you don`t approve of what I am trying to do , but , I do have a use for it.

    I will try your code and see if it is what I am looking for.

    You are a true professional for disapproving and still offering help.

    Thanks , I will let you know how it turns out.

    Marty


  • TomGoossens

    Yes I tried 0 ,1 ,2 and 3 and still got the same results.I will try again though I might have made an error of my own.
  • Alvin Kuiper

    to add a column to the datagrid, try this:

    Me.theDataGrid.Columns.Add("ColumnName", "ColumnStringName")

    Dim theRowData() as Object = {"Data1", "Data2"}

    Me.theDataGrid.Rows.Add(theRowData)

     

    this creates a column first into the datagrid, then you can add your data (row) making sure that the data/row you add, has the columns of data and should not exceed the number of columns you have added into the DataGrid Column

     

    relooking at your code, you can just databind the dataset and datagrid, if this is what you are after:

    Me.theDataGrid.DataSource = theDataSet.Tables(index).DefaultView

     

    Now to work with a DataTable, you can add columns like so:

     

    Dim theDataTable as new DataTable()

    theDataTable.Columns.Add("ColumnName", System.Type.GetType("System.YourTypeHere"))

    theDataTable.Columns("ColumnName").AutoIncrement = false

    ..

    ..

    Does this help



  • bstoker

    Ibrahim,

    I can appreciate your concern , but I never said that I was going to allow anyone else to alter the database.

    What I have in mind is to build a program that is an interface for building and populating a database , since MSDE doesn`t have one.

    I also know that there are programs out there that can aid in design but none of them are mine.

    I know about security and the consequences of allowing other people the chance of tampering with a database.

    When building a database there is always something that is forgotten : tables columns and etc.:and I just wanted a way to add these forgotten components through a visual basic program. I am not very good with sql , or for that matter VB , but I am trying to learn.

    With all of that said , Can I alter a table with a visual basic program , and add columns programmatically


  • takobell

    Me.theDataGrid.DataSource = theDataSet.Tables(index).DefaultView

    Index is not accessable because it is private.

    This is the eror message that I get when I try to run this.

    Are you using version 03 or 05 I am using 03 version.


  • Adding columns to DataGrid