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

[Solved] Creating grid in code: problem with binding...

2 Answers 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 20 Aug 2009, 02:31 PM
Help...

I am trying to create a relatively simple (but generic) report page.
Eventually, it calls a stored procedure (defined dynamically) that may return one or more recordset(s).

For each recordset, I want to create a simple grid that I simply bind to my dataset.

The user specify one or more criteria and press a button, at which time I try to create my grid(s).

My code, called from within the Button Click method looks like this (phReport is a PlaceHolder control)
        public void BuildAGrid(SqlDataReader reader)  
        {  
            RadGrid grid = new RadGrid {DataSource = reader};  
            grid.EnableViewState = false;  
            grid.DataBind();  
 
            phReport.Controls.Add(grid);  
        }  

The Grid shows and contains the right columns BUT shows "No records to display".

Initially, I had left EnableViewState as default (true) for both the PlaceHolder and the dynamically created grid and it showed the records when pressing the button a second time (I suppose the rows were then coming from the ViewState somehow as it seemed to hold the data).

The various articles I found say to create the grid in the Page_Init event but mine need to respond to the button click...

Any idea what I am doing wrong, or what I should do?

Many Thanks


Eric

2 Answers, 1 is accepted

Sort by
0
Ramakrishna Gopishetty
Top achievements
Rank 1
answered on 20 Aug 2009, 05:08 PM
Hello Eric,
I am not having enough idea here about your postbacks. If you have to select any criteria before button click which doesnt postback. Then create the Radgrid in Page_init event when page gets postback. Then bind the grid in button click event with datasource. Make it visible true in button click event and give a try. I had some problems in dynamically building grid which were overcomed by building it in page_init event. I am not sure its going to work but you can try.
0
Eric
Top achievements
Rank 1
answered on 21 Aug 2009, 10:16 AM
I am afraid it was probably me being stupid!

It seems to work with the original code now. I think I got something wrong with the parameters of my report...

Thanks for your help
Tags
Grid
Asked by
Eric
Top achievements
Rank 1
Answers by
Ramakrishna Gopishetty
Top achievements
Rank 1
Eric
Top achievements
Rank 1
Share this question
or