locked
Assigning a datasource to a gridview dynamically in the codebehind RRS feed

  • Question

  • User1441228659 posted

    Hi Guys,

    I am stuck at a task. What I want is, I am creating a table in the database on the fly in the code behind using the below code(in VB).

    Now my main problem is I want to display this table which is created in step 2 of a wizard, in step 3 using a GRIDVIEW. But as we all know a GridView needs a sqldatasource for values to be displayed, however what I need for achieving this is dynamically assign a datasource to the GridView, but I am not figuring out HOW?

    Dim NewTable As String

                Dim FullName2 As String = "Dynamic" + "_" + lblCompanyName.Text + "_" + tbCustomerFileName.Text
                'Get Row Headers and map to Fields
                readerXML.ReadHeaders()

                Dim HeaderCount As Integer = readerXML.HeaderCount()


                NewTable = "CREATE TABLE  " + FullName2 +
     "  (" +
                                    "RowId smallint IDENTITY(1,1)" +
                                   "PRIMARY KEY CLUSTERED,"
                 
                For I = 0 To HeaderCount - 1
                    'NewTable = NewTable.Replace("/", "a")
                    'NewTable = NewTable.Replace("#", "a")

                    NewTable = NewTable + readerXML.Headers(I).Replace(" ", "_").Replace("-", "_").Replace(":", "_").Replace(";", "_").Replace("%", "_").Replace("^", "_").Replace(".", "_").Replace("&", "_").Replace("*", "_").Replace("#", "_").Replace("@", "_").Replace("|", "_").Replace("\", "_").Replace("/", "_").Replace("?", "_").Replace(">", "_").Replace("<", "_").Replace("`", "_").Replace("~", "_").Replace("+", "_").Replace("=", "_").Replace(")", "_").Replace("(", "_").Replace("}", "_").Replace("{", "_").Replace("[", "_").Replace("]", "_").Replace("varchar", "_").Replace("nvarchar", "_").Replace("PRIMARY KEY", "A") + " nvarchar(MAX)  NOT NULL" +
    " DEFAULT 'New Position - title not formalized yet',"
                Next I
                NewTable = NewTable + ")"

    GridView2.datasource= ?....i am not sure what should I write here and if this line is needed at all..please advice me n this.

    GridView2.dataBind()

    So can anyone advice me on this,

    All your advice would be very helpful for me,

    Have a great weekend,

    Thanks and regards,


    Saturday, July 10, 2010 6:01 PM

Answers

  • User-1734649046 posted

    Hi,

    For GridView1.DataSource ue can assign DataTable,DataSet,SqlDataSource as follows

    GridView1.DataSource = dt;
    GridView1.DataBind();

    hope this helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 10, 2010 11:52 PM
  • User197322208 posted

    fill a data table with  FullName2

    assign to gridview2 datasource

    something like that :

    Using conn As New SqlConnection("Connection string")


    conn.Open()

      Using cmd As New SqlCommand("SELECT * FROM "+  FullName2, conn)

          Using dr As SqlDataReader = cmd.ExecuteReader()


    GridView2.DataSource = dr

        GridView2.DataBind()    

      End Using
      End Using
    End Using


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 11, 2010 12:01 AM

All replies

  • User-1734649046 posted

    Hi,

    For GridView1.DataSource ue can assign DataTable,DataSet,SqlDataSource as follows

    GridView1.DataSource = dt;
    GridView1.DataBind();

    hope this helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 10, 2010 11:52 PM
  • User197322208 posted

    fill a data table with  FullName2

    assign to gridview2 datasource

    something like that :

    Using conn As New SqlConnection("Connection string")


    conn.Open()

      Using cmd As New SqlCommand("SELECT * FROM "+  FullName2, conn)

          Using dr As SqlDataReader = cmd.ExecuteReader()


    GridView2.DataSource = dr

        GridView2.DataBind()    

      End Using
      End Using
    End Using


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 11, 2010 12:01 AM
  • User1441228659 posted

    Thanks a lot Ignatandrei,

    Your suggestion really helped me solve this task,

    Thanks a lot,

    have a great day,

    Regards,

    Sunday, July 11, 2010 9:10 PM