This is a migrated thread and some comments may be shown as answers.
DataTable Column Binding with radGridView.
13 Answers 45 Views
This is a migrated thread and some comments may be shown as answers.
Kashif
Top achievements
Rank 1
Kashif asked on 04 Aug 2019, 10:18 PM

Hi,

I'm Using radGridView to load data from Datatable;

DataTable is populated from the Database;

2 Columns are added at design time;

If i Use radGridView1.AutoGenerateColumns = true;

then it shows 4 Columns on populating data.

and if i Use radGridView1.AutoGenerateColumns = false;

then it Shows 2 columns but populated  with Empty Data Rows

01.radGridView1.MasterTemplate.AutoGenerateColumns = false;
02. 
03.DataTable dt = new DataTable();
04.string query = "SELECT a.ITEM_NAME , ITEM_CODE  FROM items a ";
05.OracleCommand cmd = new OracleCommand(query, DataConn.Conn);
06.OracleDataAdapter da = new OracleDataAdapter(query, DataConn.Conn);
07.da.Fill(dt);
08. 
09.radGridView1.MasterTemplate.DataSource = dt;
 

 

 

 

13 Answers, 1 is accepted

Sort by
0
Accepted
Nadya
Telerik team
answered on 06 Aug 2019, 07:50 AM
Hello, Muhammad,

According to the provided information I can suppose that you want to bind the RadGridView to DataTable. In RadGridView there are two ways to generate columns: automatically, according to the columns in the data source and manually with columns added by the user. The two modes can be switched using the AutoGenerateColumns property. You can find more information about this here: https://docs.telerik.com/devtools/winforms/controls/gridview/columns/generating-columns#manual-column-generation

If you set AutoGenerateColumns property to true, it means that separate columns will be automatically generated and populated with data according to information you have in your DataTable. This is the default behavior and it does not require any additional efforts from your side. 

However, if you need to control which columns to be shown in the grid, you can set  AutoGenerateColumns property to false and add the columns manually. In this case it is necessary to specify the FieldName property of the column which corresponds to the column in the DataTable. A sample example is available here: https://docs.telerik.com/devtools/winforms/controls/gridview/columns/column-types/gridviewtextboxcolumn

Please, feel free to use the approach which fits your requirements best.

I hope this information helps. Should you have any other questions do not hesitate to ask.

Regards,
Nadya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Kashif
Top achievements
Rank 1
answered on 06 Aug 2019, 12:20 PM

Thank you very much for the Reply.

its working.

0
Kashif
Top achievements
Rank 1
answered on 06 Aug 2019, 12:24 PM

One more question related to First Question.

One of my Database Column have Value Y/N.

how can i show GridView CheckBoxColumn as Checked if it has Value "Y" and UnChecked for Vakue "N"

0
Accepted
Nadya
Telerik team
answered on 09 Aug 2019, 04:51 AM
Hello, Muhammad,

To achieve this, you should implement a type converter which can convert the two values - the type that is coming from the DataSource ti the type necessary for the column and vice versa. In your case you need to implement the type converter that converts from string to bool/ToggleState type and from bool/ToggleState to string type.

The GridViewDataColumn provides a unified way of converting incompatible value types to a type supported by the column instance. More information about converting data types is available here: https://docs.telerik.com/devtools/winforms/controls/gridview/columns/converting-data-types

Let me know if you have additional questions.

Regards,
Nadya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Kashif
Top achievements
Rank 1
answered on 10 Aug 2019, 08:50 AM

Thank you So much.

It worked for me.

0
Kashif
Top achievements
Rank 1
answered on 21 Aug 2019, 06:54 PM

I have successfully Implemented the ToggleStateConvertor Class From sting to bool/ToggleState.

How can i use the Same Class for my other column which have int value like (0, 1),

Or i have to Create separate for this type;

0
Accepted
Nadya
Telerik team
answered on 23 Aug 2019, 10:14 AM
Hello, Muhammad,

Note that a Type Converter is used to convert values between different data types. Here are the four main methods that are usually used when implementing a custom Type Converter.

- Override the CanConvertFrom method that specifies which type the converter can convert from.
- Override the ConvertFrom method that implements the conversion.
- Override the CanConvertTo method that specifies which type the converter can convert to.
- Override the ConvertTo method that implements the conversion.
 
Due to the specific of the ToggleStateConverter it is necessary to implement the specific conversion depending on which types you want to convert. If you want to convert int values from another column you should create a separate TypeConverter's derivative which will convert from int to bool.

Should you have any other questions, do not hesitate to ask.

Regards,
Nadya
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Kashif
Top achievements
Rank 1
answered on 23 Aug 2019, 10:27 AM
Thank you so much.
0
Kashif
Top achievements
Rank 1
answered on 25 Sep 2019, 06:26 PM

I was Using this Code and ToggleStateConvertor class  to convert my database value to CheckBox.

GridViewCheckBoxColumn ColumnStatus = new GridViewCheckBoxColumn
{
    Name = "isActive",
    HeaderText = "",
    FieldName = "isActive",
    DataType = typeof(string),
    DataTypeConverter = new ToggleStateConverter(),
    HeaderTextAlignment = ContentAlignment.MiddleCenter,
    TextAlignment = ContentAlignment.MiddleCenter
};

 

I'm adding All the Column of the Grid manually using  the Code.

But if i add the checkboxcolumn to GridView at design time. How can i use TypeCovertor.

 

0
Accepted
Nadya
Telerik team
answered on 27 Sep 2019, 04:45 AM

Hello Kashif,

If you add the GridViewCheckBoxColumn at design time you should access it as shown below:

 this.radGridView1.Columns["newColumn"].DataTypeConverter = new ToggleStateConverter();

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Nadya
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Kashif
Top achievements
Rank 1
answered on 28 Sep 2019, 01:28 PM

Thank you Nadya.

This is only applicable through Coding. Or i can set it while designing.

0
Nadya
Telerik team
answered on 01 Oct 2019, 02:07 PM

Hello Kashif,

The DataTypeConverter property is not available in the designer, so you will need to set it at run time in the code.

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

Regards,
Nadya
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Kashif
Top achievements
Rank 1
answered on 01 Oct 2019, 07:18 PM
Thank you very much.
Asked by
Kashif
Top achievements
Rank 1
Answers by
Nadya
Telerik team
Kashif
Top achievements
Rank 1
Share this question
or