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

Problems with column generation

9 Answers 140 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Vitas Vitauskas
Top achievements
Rank 1
Vitas Vitauskas asked on 15 Dec 2009, 07:41 AM
Hello, I want to generate columns of grid automaticaly. Everything works good except that generated colums have no data. Here is my code:
            try
{
LoadDataTable = SqlHelper.GetTable(LoadProcedureSelectName, Args);
}
catch (Exception e)
{
CreateSelectProcedure(LoadProcedureSelectName);
}
LoadData(LoadDataTable);

protected void LoadData(DataTable dataTable)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataMember = null;
            dataGridView1.Columns.Clear();
            LoadDataTable = dataTable;
            dataGridView1.ItemsSource = LoadDataTable.DefaultView;
            if (dataGridView1.Columns.Count == 0)
                GenerateColumns();
}
        protected void GenerateColumns()
        {
            dataGridView1.Columns.Clear();
            dt = SqlHelper.GetTable("base_Fields_SelectVisible", new string[] { "@UserID", Globals.UserID, "@TableName", _TableName });
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["Type"].ToString().Equals("string"))
                {
                    GenerateTextBoxWPF(dr);
                }
            }
        }

private void GenerateTextBoxWPF(DataRow dr)
        {
            GridViewColumn col = new GridViewColumn();
            col.Name = dr["FieldName"].ToString();
            col.Header = dr["Caption"].ToString();
            col.UniqueName = dr["FieldID"].ToString();
            col.IsReadOnly = true;
            col.Width = Convert.ToInt32(dr["width"].ToString());

            dataGridView1.Columns.Add(col);
            if (dr["Visible"].ToString() == "1")
            {
                dataGridView1.Columns[dataGridView1.Columns.Count - 1].IsVisible = true;
            }
            else
            {
                dataGridView1.Columns[dataGridView1.Columns.Count - 1].IsVisible = false;
            }
}

What i do wrong. This technique worked very well with standart wpf grid and also with devExpress grid.


9 Answers, 1 is accepted

Sort by
0
Tsvyatko
Telerik team
answered on 15 Dec 2009, 12:26 PM
Hello Vitas Vitauskas,

 To enable binding using UniqueName property you need to make the following modification:

Original code:

GridViewColumn col = new GridViewColumn();

Modified Code:

Telerik.Windows.Controls.GridViewColumn col = new Telerik.Windows.Controls.GridViewDataColumn();


Hope this helps.

Best wishes,
Tsvyatko Konov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Vitas Vitauskas
Top achievements
Rank 1
answered on 15 Dec 2009, 12:56 PM
This dont helped - headers are sown, but cells are still empty (without data).
Maybe there is some other and better way to generate my colums of grid, depending of visible fields, that are grabbed for each app user individualy ant then generated with data.
0
Tsvyatko
Telerik team
answered on 15 Dec 2009, 05:21 PM
Hello Vitas Vitauskas,

 Attached you will find a sample project which uses your code to displays the Grid View data.

Additionally you may consider using the column property DataMemberBinding which provides more flexible way to bind the data using Bindings. (See the commented code in GenerateTextBoxWPF method).

Also you can look at option for auto generating columns and using AutoGeneratingColumn event to customize each column.

If you have any more questions do not hesitate to contact us.


Best wishes,
Tsvyatko
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Vitas Vitauskas
Top achievements
Rank 1
answered on 16 Dec 2009, 01:48 PM
Hm, i tried your example, but only when i replace:
            DataTable dt = new DataTable(); 
            dt.Columns.Add(new DataColumn("Id"));
           dt.Columns.Add(new DataColumn("Name"));
            FillData(dt);

           
with:

DataTable dt = SqlHelper.GetTable(LoadProcedureSelectName, Args);
 return dt;

This does not works anymore. So i think this is not a universal way to desision as i wanted ,becouse method "private DataTable GenerateColumnTypes()" "hardcodes" my columns. Columns i think must be generated depending of DataTable dt, which comes from SQL.
0
Tsvyatko
Telerik team
answered on 18 Dec 2009, 12:41 PM
Hello Vitas Vitauskas,

 Can you provide additional information about datatable content?

About your concern that column types are hardcoded - you can take advantage of an ability in Gridview data column - it can automatically resolve property type and display the controls needed (e.g. for boolean properties  - checkbox is displayed)

In addition you can enable AutogenerateColumn=true and attach to AutogeneratingColumn event to customize the column as needed.

Kind regards,
Tsvyatko
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Vitas Vitauskas
Top achievements
Rank 1
answered on 21 Dec 2009, 10:14 AM
Its not necesarily to provide information about datatable content, becouse datatable content can be different in earch datatable, So this method must accept every datatable, undependently of its content.
AutogenerateColumns - this decision not for me, becouse every time the user changes his visible columns and column order, i save that info to SQL, so the user always see columns, he wants to see and  he has full capability to modify visible columns.
0
Tsvyatko
Telerik team
answered on 21 Dec 2009, 02:29 PM
Hello Vitas Vitauskas,

As I understand you need to design the grid in such a way that it will be able to visualize data without knowing its fields. In addition you will load saved data about grid columns current state.
 I assume that there is some correlation between data that will be visualized and saved settings data (e.g. you keep saved settings about how the grid looks like when displaying Orders data - 5 columns, two invisible with specific width for each column; another set of settings which keeps track of how the grid will look like when displaying Costumers data - 10 columns, 5 visible with specific width).

Thus I have extended the example and have added comments how you can bind your grid with any data (two different data tables are used to illustrate the idea) and loads specific settings depending on data loaded. The grid view does not have any knowledge about contents of data displayed.

Please let know if this works for you.

Sincerely yours,
Tsvyatko
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Vitas Vitauskas
Top achievements
Rank 1
answered on 22 Dec 2009, 08:48 AM
Hi, I changed code from yuor attachment like this:
protected void LoadData() 
        { 
            DataTable table = null
 
            table = SqlHelper.GetTable(LoadProcedureSelectName, Args); 
 
            dataGridView1.AutoGenerateColumns = false
            dataGridView1.DataMember = null
            dataGridView1.Columns.Clear(); 
            if (dataGridView1.Columns.Count == 0) 
                GenerateColumns(table); 
            dataGridView1.ItemsSource = table.DefaultView; 
 
 
        } 
 
        protected void GenerateColumns(DataTable dataTable) 
        { 
            dataGridView1.Columns.Clear(); 
            //get the data for the column settings from the database 
            DataTable columnTypes = SqlHelper.GetTable("base_Fields_SelectVisible", new string[] { "@UserID", Globals.UserID, "@TableName", _TableName }); 
            foreach (DataRow dr in columnTypes.Rows) 
            { 
                GenerateColumn(dr); 
            } 
        } 
 
 
 
 
        private void GenerateColumn(DataRow dr) 
        { 
            Telerik.Windows.Controls.GridViewDataColumn col = new Telerik.Windows.Controls.GridViewDataColumn(); 
            col.Name = dr["FieldName"].ToString(); 
            col.Header = dr["Caption"].ToString(); 
 
            //col.DataMemberBinding = new Binding(dr["FieldID"].ToString()); 
            col.UniqueName = dr["FieldID"].ToString(); 
 
 
            col.IsReadOnly = true
            col.Width = Convert.ToInt32(dr["width"].ToString()); 
 
            dataGridView1.Columns.Add(col); 
            if (dr["Visible"].ToString() == "1") 
            { 
                dataGridView1.Columns[dataGridView1.Columns.Count - 1].IsVisible = true
            } 
            else 
            { 
                dataGridView1.Columns[dataGridView1.Columns.Count - 1].IsVisible = false
            } 
        } 
Columns are generated, but cells stil are empty (without data). Im attaching image of table, whitch contains information about visible columns(Base_Fields). Also i attach my original table with some fields (not all fitted to an image), but this table is only one of many available tables, whitch can be more different.
0
Tsvyatko
Telerik team
answered on 23 Dec 2009, 01:51 PM
Hi Vitas Vitauskas,

Can you please check whether the application runs without any exceptions in the output window in debug mode?
 If it is not, can you please provide that part of your project that uses GridView including database schema and SQLHelper class, so that I can modify it to work correctly.


Sincerely yours,
Tsvyatko
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Vitas Vitauskas
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
Vitas Vitauskas
Top achievements
Rank 1
Share this question
or