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
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
Thank you very much for the Reply.
its working.
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.
Regards,
Nadya
Progress Telerik
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.
Regards,
Nadya
Progress Telerik
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.
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
Thank you Nadya.
This is only applicable through Coding. Or i can set it while designing.
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