This is a migrated thread and some comments may be shown as answers.

[Solved] create grid columns programmatically

4 Answers 132 Views
Grid
This is a migrated thread and some comments may be shown as answers.
bgoldca
Top achievements
Rank 1
bgoldca asked on 17 Sep 2009, 07:58 PM
I have a grid that's declared in the ASPX and I want to create the columns in the codebehind. I think I'm doing exactly waht's in the documentation but all I get is an empty box displaying. Here's my code:

In the ASPX:

<radG:RadGrid ID="RadSummary" runat="server" AllowSorting="True" AutoGenerateColumns="false">

 

 

    <MasterTableView AutoGenerateColumns="false" AllowNaturalSort="False"></MasterTableView>

 

 

    <ClientSettings AllowColumnsReorder="true" ApplyStylesOnClient="False" EnablePostBackOnRowClick="false">

 

 

        <Selecting AllowRowSelect="false" />

 

 

    </ClientSettings>

 

 

</radG:RadGrid>


In the code-behind in the clicked event of a button I have:

GridBoundColumn

 

boundColumn = new GridBoundColumn();

 

RadSummary.MasterTableView.Columns.Add(boundColumn);

boundColumn.HeaderText = "Column 1;

 

 

RadSummary.DataBind();

 


I want this grid to display with no datarows - just the column headings, but when the event fires to add the columns nothing (visible) happens. What am I doing wrong - does the grid need to be bound to some datasource before it will display?

 

 

4 Answers, 1 is accepted

Sort by
0
Schlurk
Top achievements
Rank 2
answered on 17 Sep 2009, 08:34 PM
You could also attempt to do this initially with a DataTable, and then set this as the source of the grid (this might not be the alternative you are looking for but it's a way of approaching this). An example of this would be:
DataTable myTable = new DataTable(); 
myTable.Columns.Add("Column1"); 
DataRow myRow = myTable.NewRow(); 
myRow.ItemArray[0] = "Test"
myTable.Rows.Add(myRow); 
RadGrid1.DataSource = myTable; 

0
bgoldca
Top achievements
Rank 1
answered on 17 Sep 2009, 09:15 PM
I may end up going the datatable route (it's looking like a good option right about now!). If I do that how can I set the column's display properties. I'm trying this now and the column(s) are now the right width and are justified incorrectly (a string is showing up left justified).

Thanks.
0
Shinu
Top achievements
Rank 2
answered on 18 Sep 2009, 07:14 AM
Hi bgoldca,

Give a try with the following approach.

CS:
 
 protected void Button1_Click(object sender, EventArgs e) 
    { 
        
        GridBoundColumn boundColumn = new GridBoundColumn(); 
        RadSummary.MasterTableView.Columns.Add(boundColumn); 
        boundColumn.HeaderText = "Column 1"
 
        //set the Grid DataSource 
        RadSummary.DataSource = new string[] { }; 
        RadSummary.DataBind();  
          
    } 


Thanks
Shinu
0
bgoldca
Top achievements
Rank 1
answered on 18 Sep 2009, 04:26 PM
Thank you! That worked.
Tags
Grid
Asked by
bgoldca
Top achievements
Rank 1
Answers by
Schlurk
Top achievements
Rank 2
bgoldca
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or