0

I am working on a desktop application (VB.NET). I have a form with the Dev Express Xtra Grid. Within the grid, I have first column as unbound check box (Repository Item). Right now I have made the functionality so that user can check/un-check the check boxes to select the rows.

What I want to achieve is:

  1. I have a button on the form. If no check boxes are checked the button should be disabled else it should be enabled. By default when form loads, all the check boxes are checked and so button is enabled. User can un-check all the check boxes. So how can I find if any one check box is checked or not to make the button enable/disable.

  2. On button click event, I want to get the id (this is the column within the grid next to check box) of all the rows which have check box checked.

I do have the idea that I have to first make the for loop for all the rows within the grid and then for loop for the check boxes to find the check box checked or not. However not making it putting together.

Please help me guys to fix the issue.

Thanks

manish chavda
  • 19
  • 1
  • 3

2 Answers2

0

am not sure about "Dev Express Xtra Grid". Still if u use a data grid

First declare and initialize a variable say, flag - false, to false

Now u can use a for loop which continues looping until the number of rows in the grid

inside the loop, u check whether the check box of "current column" is checked

if Checked then set the flag to true and break from the loop else continue the loop

after the loop check for the current flag value if flag=false then disable the button else enable the button.

For your second question do the same loop and

inside loop check whether the current row is checked if true take the value of id column else continue.

I think this logic may help you. :)

Sin
  • 1,836
  • 2
  • 17
  • 24
  • Thanks for your information. I did have the same idea you have mentioned here but I was looking for some code to do so... Nevermind I have fixed the issue and able to solve all the above issues... – manish chavda Nov 09 '12 at 05:44
0

If you're using the XtraGrid GridControl, you want to deal more with the GridView, which is the editor contained within the GridControl.

Typically, you'll bind your data to the GridControl's DataSource Property, but most of the other events and properties that you'll want to use for user experience will be related to the GridView itself.

Some of the handier methods and properties you get with the GridView are FocusedRowHandle, FocusedColumn, GetFocusedRow(), etc.

So, when you register your click event for that button, inside that method, store a reference to the gridview, i.e.

private void SomeButtonClick(object sender, EventArgs e)
{
     var gridView = this.whateverYourGridViewIsNamedGridView;

     //Now, you can access the methods and properties of the gridView...

     //Say you want to obtain the focused row's handle
     var rowHandle = gridView.FocusedRowHandle;

     //Or, in your case, if you want to iterate through the rows or columns...
     for(GridColumn column in gridView.Columns)
     {
          if(condition)
          {
               //Do something
          }
     }
}

Based on your scenario, I would suggest that you again open the Designer. On the bottom left, click In-Place Editor Repository. You should see your CheckEdit here. If you select the CheckEdit, you should be able to click the little lightning bolt and access the editor's events. You want to register with the CheckStateChanged event or the CheckedChanged event, which will fire any time any of the editor's check state's are changed.

From here, I would add a bool to your domain object or a viewmodel to decorate that domainobject with bool on it for isChecked. This way, when the check event fires, you can handle setting this bool... for example:

private void CheckEventFiring(object sender, EventArgs e)
{
    //Get the currently focused row and cast it to your object
    //This will expose all the properties, including the aforementioned boolean value
    var currentRow = gridView.GetFocusedRow() as YourDomainObject; 

    //Based on checked state...
    currentRow.IsChecked = //Checked or Unchecked    
}

Now that you've set this, when you click the button, you can just get all the items from your grid control's data source that "are checked" by doing something like...

var dataSource = gridControl.DataSource as List<YourDomainObject>().Where(x => x.IsChecked);

Now you only have the data from the rows where the items are checked. When check state is unchecked, the bool on the object should be false, when checked it should be true.

Let me know if this makes sense or not. Dev Express has a small learning curve, but once you get it, it's pretty easy.

Ry-
  • 218,210
  • 55
  • 464
  • 476
captray
  • 3,618
  • 1
  • 18
  • 15