unbound DataGridViewCheckBoxColumn


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: unbound DataGridViewCheckBoxColumn

  1. #1
    Join Date
    Feb 2009
    Posts
    5

    unbound DataGridViewCheckBoxColumn

    I have a question about DataGridView and unbound DataGridViewCheckBoxColumn.

    On the Form_Load I get a Dataset and display it in the Grid.
    One of the Dataset fields has string value either "Yes" or "No".
    I would like to display that column as a DataGridViewCheckBoxColumn,
    1) remove that column; 2) add DataGridViewCheckBoxColumn; 3) change DataGridViewCheckBoxColumn's value depending on the Dataset field's value.

    Pretty simple. Let's say that field has the index 3.
    Code:
    //Get Dataset ds
    dGrid.DataSource = ds.Tables[0];
    
    dGrid.Columns.Remove("YesNoField");
                
    DataGridViewCheckBoxColumn ch = new DataGridViewCheckBoxColumn();
    ch.Name = "YesNoField";
    dGrid.Columns.Insert(3, ch);
                
    DataGridViewRow row;
    for (int i = 0; i < dGrid.Rows.Count; i++)
    {
            row = dGrid.Rows[i];
    
            if (ds.Tables[0].Rows[i].ItemArray[3].ToString() == "Yes")
            {
                row.Cells["YesNoField"].Value = true;
            }
            else
            {
                row.Cells["YesNoField"].Value = false;
            }
    }
    The problem is that this code doesn't work on Form_Load:
    Column is not removed and DataGridViewCheckBoxColumn values are null
    But if I have a button on the form and call the same code on the button click, it works fine.

    Can someone tell me what is the difference between Form_Load and Button_Click for this scenario and how to make it working on Form_Load?

    Please don't tell me that it'd be easier to have a boolean field in the database. I use SQLite which doesn't support boolean datatype.
    Last edited by Hack; 04-30-2009 at 12:22 PM. Reason: Added Code Tags

  2. #2
    Join Date
    Jul 2008
    Location
    Perth
    Posts
    18
    Why not use the datagridview Cellformatting event rather than form load?
    Make sure the column you want to change is ahead of the column containing the info.

  3. #3
    Join Date
    Feb 2009
    Posts
    5
    Thanks but that will only help to format cells. I still can't remove the column. Of course, I can make it invisible but just wondering if it's possible to do something with DataGridView on Load event.

  4. #4
    Join Date
    Mar 2009
    Posts
    4

    Having the same problem

    I have exactly the same problem as you experience.
    The Datagridview is created in the windows application and after initilializeComponents I add data to the dataGrid and also the checkbox. But I cannot set the checkbox values to true or false.

    Bur If I press the button that is in the application it can set the values.
    But I want them default to be set to true but the won't...
    Can someone please help me I have searched the whole internet for an answer but I haven't found one yet...

  5. #5
    Join Date
    Feb 2009
    Posts
    5
    well, if you want to format only your checkbox, then use CellFormatting event like macowl. Works fine.

    (If you wanted values to be false by default, then you'd not need to do anything - you'd just check if a value is null)

  6. #6
    Join Date
    Mar 2009
    Posts
    4
    Thx for fast response but how can the cellFormatting help me in this case?
    I add the datagridviewcheckboxcolumn as:

    DataGridViewCheckBoxColumn checkCol = new DataGridViewCheckBoxColumn();
    checkCol.HeaderText = "Use";
    checkCol.Name = "Use";
    checkCol.CellTemplate = new DataGridViewCheckBoxCell();
    dataGridViewTraining.Columns.Insert(0, checkCol);

    I then set the first value in the checkbox to true and check that it is really set:

    dataGridViewTraining.Rows[0].Cells[0].Value = true;
    if ((bool)dataGridViewTraining.Rows[0].Cells[0].Value == true)
    {
    and in the debugger the if statement is true.
    But the when the application has started then If I do exactly the same if check when the button is pressed then the value is null??? why has it changed?

  7. #7
    Join Date
    Feb 2009
    Posts
    5

    Thumbs up

    To be honest I am not sure of syntax but try something like this:

    dataGridView1_CellFormatting(object sender, e EventArgs)
    {
    //assuming that your checkbox is the first column
    if (e.ColumnIndex == 0)
    e.Value = true;
    }

  8. #8
    Join Date
    Mar 2009
    Posts
    4
    Problem solved!
    It seems that it was the way I added the other columns that caused my fault.
    I created a table first and added the data by adding the Tble to dataSource and then I added a checkbox column.

    If I instead created the columns by only doing:
    dataGridViewTraining.Columns.Add(column);
    dataGridViewTraining.Rows.Add(new string[] { "" });

    //and then set the values in the datagrdiview by:
    dataGridViewTraining.Rows[0].Cells[0].Value = "tjoho";

    //and then adding the checkboxColumn:
    DataGridViewCheckBoxColumn checkCol = new DataGridViewCheckBoxColumn();
    checkCol.CellTemplate = new DataGridViewCheckBoxCell();
    checkCol.HeaderText = "Use";
    checkCol.Name = "Use";
    checkCol.ThreeState = false;
    checkCol.FalseValue = false;
    checkCol.TrueValue = true;

    dataGridViewTraining.Columns.Insert(0, checkCol);

    dataGridViewTraining.Rows[0].Cells[0].Value = true ;

    Then it works!!!
    It has to do something width bounded and unbounded data but I can't understand why I couldnät get it to work somehow but that doesn't matter now! everything works!

  9. #9
    Join Date
    Feb 2009
    Posts
    5

    Still looking for datagridview check box solution

    I'm not sure what you were trying to say but if I understood you correct, then it's not even close to be a nice solution.

    Most of the time we get data into a grid from underlying datasource with no knowledge about columns and cells.

    In your solution you simply suggest instead of (assuming that ds is a DataSet)
    single line -

    dGrid.DataSource = ds.Tables[0];

    use something like this -

    for (j = 0; j < ds.Tables[0].Columns.Count; j++)
    dGrid.Columns.Add(ds.Tables[0].Columns[j].ColumnName);

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
    DataGridViewRow row = new DataGridViewRow();
    string newRowString = "";

    for (j = 0; j < ds.Tables[0].Columns.Count; j++ )
    {
    newRowString += ds.Tables[0].Rows[i].ItemArray[j].ToString();
    if (j < ds.Tables[0].Columns.Count - 1)
    newRowString += ", ";
    }

    dGrid.Rows.Add(newRowString);
    }

    Can't say for other developers but I don't think that I want to do it every time.

  10. #10
    Join Date
    Mar 2009
    Posts
    4
    Yes I know its a horrible solution and it has to be another way around it.

    I have other datagridviews that uses dataSource but in them I haven't added a datagridviewcheckbox.

    In the datagridview that I use here I only have one column which always is the same and it only has 9 rows so it is simple to get around the problem in the way that I described.

    I hope to solve the original problem so that I can use a datasource and then add the datagridview but it seems that those values that aren't bound will be resetted if I mix.

    In the code I first added data to the grid with dataSource and those data will be bounded but I guess that the checkboxvalues won't be in my case and then they will be resetted, thats the only explaination I can come up with, is there anyone else that have a good explaination? I am only guessing now.

  11. #11
    Join Date
    Apr 2009
    Posts
    1
    try this,
    checkCol.ValueType = typeof(string);
    checkCol.ThreeState = false;
    checkCol.FalseValue = "No";
    checkCol.TrueValue = "Yes";
    hope is helpfull

Similar Threads

  1. Reading unbound label
    By Vulcan1500 in forum Database
    Replies: 5
    Last Post: 04-18-2008, 08:36 AM
  2. Edit a unbound column in datagridview
    By DaPosh in forum .NET
    Replies: 0
    Last Post: 07-17-2006, 08:11 PM
  3. .NET unbound grid
    By Phil Weber in forum .NET
    Replies: 5
    Last Post: 12-01-2001, 06:53 AM
  4. Using grid control in Unbound mode
    By Paul B in forum VB Classic
    Replies: 0
    Last Post: 11-09-2000, 05:04 PM
  5. Loading a TDBgrid in unbound mode
    By bb in forum VB Classic
    Replies: 1
    Last Post: 06-02-2000, 04:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center