locked
Populate GridView without DataSource RRS feed

  • Question

  • User-194155210 posted

    I want to populate a GridView without using a DataSource.  I want to created a Protected Sub that will take information I have calculated and display it on multiple rows (5 to be specific).  Can this be done easly?

    Monday, July 16, 2012 8:37 AM

Answers

  • User-1660800924 posted
    Module Module1
        Sub Main()
    	' Get a DataTable instance from helper function.
    	Dim table As DataTable = GetTable()
        End Sub
    
     Function GetTable() As DataTable
    	' Create new DataTable instance.
    	Dim table As New DataTable
    	' Create four typed columns in the DataTable.
            table.Columns.Add("column name",column type)
    	table.Columns.Add("column name",column type)
    	' Add rows with those columns filled in the DataTable.
            for example
    	table.Rows.Add(25, "Indocin", "David", DateTime.Now) 
    	table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
    	
    	Return table
        End Function
    End Module
    then bind the table to the Gridview 
    GridView1.DataSource = dt
       
    GridView1.DataBind()
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 16, 2012 8:51 AM
  • User-194155210 posted

    Had to add this line of code to fix it not showing up.

    TuitionGridView.AutoGenerateColumns = True



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 17, 2012 5:46 PM

All replies

  • User-672832619 posted

    It means you want to fill grid using through code not directly throug DataSource?? Right?

    Monday, July 16, 2012 8:43 AM
  • User-2107542234 posted

    you can put your results in a DataTable then make the data source your DataTable.

    hope this helps you.

    regards.

    Monday, July 16, 2012 8:43 AM
  • User-672832619 posted
    // May be this can help you.
    
    
    public void fillGrid()
    
    {
    
    con.Open();
    string sql = "select *from contact";
    cmd = new SqlCommand(sql, con);
    dt = new DataTable();
    da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    grid_contact.DataSource = dt;
    grid_contact.DataBind();
    cmd.Dispose();
    con.Close();
    
    }



    Monday, July 16, 2012 8:45 AM
  • User-1660800924 posted
    Module Module1
        Sub Main()
    	' Get a DataTable instance from helper function.
    	Dim table As DataTable = GetTable()
        End Sub
    
     Function GetTable() As DataTable
    	' Create new DataTable instance.
    	Dim table As New DataTable
    	' Create four typed columns in the DataTable.
            table.Columns.Add("column name",column type)
    	table.Columns.Add("column name",column type)
    	' Add rows with those columns filled in the DataTable.
            for example
    	table.Rows.Add(25, "Indocin", "David", DateTime.Now) 
    	table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
    	
    	Return table
        End Function
    End Module
    then bind the table to the Gridview 
    GridView1.DataSource = dt
       
    GridView1.DataBind()
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 16, 2012 8:51 AM
  • User-194155210 posted

    I'm a little confused on how to implement this.

    I tried the above code but I was not able to reference any of my Protected Sub's to populate some of the rows.

    Monday, July 16, 2012 9:50 AM
  • User-194155210 posted

    Module Module1
        Sub Main()
    	' Get a DataTable instance from helper function.
    	Dim table As DataTable = GetTable()
        End Sub
    
     Function GetTable() As DataTable
    	' Create new DataTable instance.
    	Dim table As New DataTable
    	' Create four typed columns in the DataTable.
            table.Columns.Add("column name",column type)
    	table.Columns.Add("column name",column type)
    	' Add rows with those columns filled in the DataTable.
            for example
    	table.Rows.Add(25, "Indocin", "David", DateTime.Now) 
    	table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
    	
    	Return table
        End Function
    End Module
    then bind the table to the Gridview 
    GridView1.DataSource = dt    GridView1.DataBind()
    

    I'm confused on the Binding part.   Where is dt coming from?

    Monday, July 16, 2012 11:41 AM
  • User-1660800924 posted

    Actually the line of code should be 

    Gridview1.Datasource = table; // table which you are declarating on Main

    Gridview1.databind();

    My bad 

    Monday, July 16, 2012 1:30 PM
  • User-194155210 posted

    When I have the code you provided in the Module Module 1, I can't access any of my other functions in my Partial Class _Default.   Any suggestions?

    Tuesday, July 17, 2012 1:35 PM
  • User-194155210 posted

    Had to add this line of code to fix it not showing up.

    TuitionGridView.AutoGenerateColumns = True



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 17, 2012 5:46 PM