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

get the datafield from the RadGrid.column

3 Answers 694 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 10 Jan 2010, 09:52 PM
Hi,  radgrid
    I wish to get the value of the datafield of a column programmatically, however when I type Columns[0]., intellisence
does not show the datafield option. I have to use the Uniquename instead, however this will not always be the same as the datafield and thus could cause problems. Please help.




GridFilteringItem item = MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;
                
             
                for (int i = 0; i < Columns.Count - 1; i++)
                    {

                    Type colType = Columns[i].DataType;
                    Telerik.Web.UI.GridColumn column = Columns[i];
                    string colName = MasterTableView.Columns[i].UniqueName;

                    if (colType == typeof(System.String) && (Columns[i].Visible))
                    {

3 Answers, 1 is accepted

Sort by
0
Schlurk
Top achievements
Rank 2
answered on 11 Jan 2010, 05:58 PM
A generic GridColumn object does not contain the DataField property. You will have to cast this column as a GridBoundColumn in order to retrieve this information:

 
GridBoundColumn myColumn = (GridBoundColumn)RadGrid1.Columns[0]; 
String myString = myColumn.DataField; 

0
Mohan
Top achievements
Rank 2
answered on 14 Nov 2012, 03:38 AM
Ok Schlurk,
    After getting the DataField name using the code
GridBoundColumn myColumn = (GridBoundColumn)RadGrid1.Columns[0]; 
String myString = myColumn.DataField; 

how will i dynamically set the column name to the corresponding grid control?

string errMsg = string.Empty;
CarFamilies listDAL = new CarFamilies();
CarFamiliesDTO ListDTO = new CarFamiliesDTO();
ListDTO = listDAL.GetCarDetails(ref errMsg, ListDTO);
DataTable dt1 = ListDTO.dtCarFamilies;
grdDynamic.DataSource = dt1;
grdDynamic.Columns[0].HeaderText = "CAR CODE";
grdDynamic.Columns[1].HeaderText = "CAR NAME";
GridBoundColumn myColumn = (GridBoundColumn)grdDynamic.Columns[0];
String mystring = myColumn.DataField;
GridBoundColumn myCol = (GridBoundColumn)grdDynamic.Columns[1];
String mystr = myCol.DataField;
grdDynamic.DataBind();


Schlurk, i will be glad if you help me to sort out my problem as soon as possible
0
Andrey
Telerik team
answered on 19 Nov 2012, 07:20 AM
Hi,

If you use autogenerated column they will take the name of the DataField as unique name. You could hook the ColumnCreated event of RadGrid and change other properties if necessary.

On the other hand you could define the columns programmatically as shown in this help topic.

I do not see where you are using the mystring and mystr variables? What is your final goal?

Greetings,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Mark
Top achievements
Rank 1
Answers by
Schlurk
Top achievements
Rank 2
Mohan
Top achievements
Rank 2
Andrey
Telerik team
Share this question
or