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
13 Answers, 1 is accepted
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.
Thank you very much for the Reply.
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"
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.
Thank you So much.
It worked for me.
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;
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.
Should you have any other questions, do not hesitate to ask.
I was Using this Code and ToggleStateConvertor class to convert my database value to CheckBox.
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.
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.
Thank you Nadya.
This is only applicable through Coding. Or i can set it while designing.
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.