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

Grid issue With columns

3 Answers 92 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 28 Oct 2011, 09:33 PM
I am populating a grid from a datatable. I have to populate the columns from a database table to allow dynamic changing of fields and names:


                                foreach (DataRowView drv in dt.DefaultView)
                                {
                                    GridBoundColumn boundColumn;
                                    boundColumn = new GridBoundColumn();
                                    boundColumn.DataField = drv["DATA_FIELD"].ToString();
                                    boundColumn.UniqueName = drv["DATA_FIELD"].ToString();
                                    string x = L10N.Term(Context.Application, "test", drv["HEADER_TEXT"].ToString());
                                   
                                    boundColumn.HeaderText = L10n.Term(x);
                                    RadGrid1.MasterTableView.Columns.Add(boundColumn);


                                   
                                }

Even though I have told it what columns I want it to have it populates all the columns from the SQL search string. which has "Select * from "

Am I missing something?

3 Answers, 1 is accepted

Sort by
0
Accepted
TIM
Top achievements
Rank 1
answered on 28 Oct 2011, 11:31 PM
after your loop, are you setting the datasource and/or binding it?
0
Princy
Top achievements
Rank 2
answered on 29 Oct 2011, 06:20 AM
Hello Tim,

I suppose you want to make change in column header and fields after populating the DataTable. So you can try the following code snippet.

C#:
protected void Page_Init(object sender, EventArgs e)
 {
       PlaceHolder PlaceHolder1 = new PlaceHolder();
       RadGrid RadGrid1 = new RadGrid();
       RadGrid1.ID = "RadGrid1";
       form1.Controls.Add(PlaceHolder1);
       form1.Controls.Add(RadGrid1);
       RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
       RadGrid1.PageSize = 15;
       RadGrid1.ClientSettings.Scrolling.UseStaticHeaders = true;
       RadGrid1.AllowPaging = true;
       RadGrid1.AutoGenerateColumns = false;
       con.Open();
       string s = "select EmployeeID,FirstName,LastName,Title,Address from Employees";
       SqlDataAdapter dr = new SqlDataAdapter(s, con);
       dr.Fill(dt);
       con.Close();
       RadGrid1.DataSource = dt;
       RadGrid1.MasterTableView.DataKeyNames = new string[] { "EmployeeID" };
       GridBoundColumn boundColumn = new GridBoundColumn();
       boundColumn.DataField = "EmployeeID";
       boundColumn.HeaderText = "EmployeeID1";
       boundColumn.HeaderStyle.Width = 70;
       boundColumn.HtmlEncode = false;
       RadGrid1.MasterTableView.Columns.Add(boundColumn);
       boundColumn = new GridBoundColumn();
       boundColumn.DataField = "FirstName";
       boundColumn.HeaderText = "FirstName1";
       boundColumn.HeaderStyle.Width = 70;
       boundColumn.HtmlEncode = false;
       RadGrid1.MasterTableView.Columns.Add(boundColumn);
       boundColumn = new GridBoundColumn();
       boundColumn.DataField = "LastName";
       boundColumn.HeaderText = "LastName1";
       boundColumn.HeaderStyle.Width = 70;
       boundColumn.HtmlEncode = false;
       RadGrid1.MasterTableView.Columns.Add(boundColumn);
       boundColumn = new GridBoundColumn();
       boundColumn.DataField = "Title";
       boundColumn.HeaderText = "Title1";
       boundColumn.HeaderStyle.Width = 70;
       boundColumn.HtmlEncode = false;
       RadGrid1.MasterTableView.Columns.Add(boundColumn);      
 }

Thanks,
Princy.
0
Michael
Top achievements
Rank 1
answered on 01 Nov 2011, 02:46 PM
Yes it was because I was Binding the data after I setup the columns... Thanks everyone!
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
TIM
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Michael
Top achievements
Rank 1
Share this question
or