DataTable Column Binding with radGridView.

14 posts, 4 answers
  1. Kashif
    Kashif avatar
    66 posts
    Member since:
    Jan 2019

    Posted 04 Aug 2019 Link to this post

    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;
     

     

     

     

  2. Answer
    Nadya
    Admin
    Nadya avatar
    166 posts

    Posted 06 Aug 2019 Link to this post

    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.
  3. Kashif
    Kashif avatar
    66 posts
    Member since:
    Jan 2019

    Posted 06 Aug 2019 in reply to Nadya Link to this post

    Thank you very much for the Reply.

    its working.

  4. Kashif
    Kashif avatar
    66 posts
    Member since:
    Jan 2019

    Posted 06 Aug 2019 Link to this post

    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"

  5. Answer
    Nadya
    Admin
    Nadya avatar
    166 posts

    Posted 08 Aug 2019 Link to this post

    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.
  6. Kashif
    Kashif avatar
    66 posts
    Member since:
    Jan 2019

    Posted 10 Aug 2019 in reply to Nadya Link to this post

    Thank you So much.

    It worked for me.

  7. Kashif
    Kashif avatar
    66 posts
    Member since:
    Jan 2019

    Posted 21 Aug 2019 in reply to Nadya Link to this post

    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;

  8. Answer
    Nadya
    Admin
    Nadya avatar
    166 posts

    Posted 23 Aug 2019 Link to this post

    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.
  9. Kashif
    Kashif avatar
    66 posts
    Member since:
    Jan 2019

    Posted 23 Aug 2019 in reply to Nadya Link to this post

    Thank you so much.
  10. Kashif
    Kashif avatar
    66 posts
    Member since:
    Jan 2019

    Posted 25 Sep 2019 in reply to Nadya Link to this post

    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.

     

  11. Answer
    Nadya
    Admin
    Nadya avatar
    166 posts

    Posted 26 Sep 2019 Link to this post

    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.
  12. Kashif
    Kashif avatar
    66 posts
    Member since:
    Jan 2019

    Posted 28 Sep 2019 in reply to Nadya Link to this post

    Thank you Nadya.

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

  13. Nadya
    Admin
    Nadya avatar
    166 posts

    Posted 01 Oct 2019 Link to this post

    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.
  14. Kashif
    Kashif avatar
    66 posts
    Member since:
    Jan 2019

    Posted 01 Oct 2019 in reply to Nadya Link to this post

    Thank you very much.
Back to Top