Access to all checked RadGridView 's Rows via GridViewCheckBoxColumn (Telerik component) - c#

i want to access all checked row in RadGridView(Telerik component) via GridViewCheckBoxColumn. Note : User select checkbox at run time ...

you can access checkbox column like this:
foreach (GridViewRowInfo item in radGridView1.Rows.ToList())
{
if(item.Cells[0].Value.Equals(true))
{
//dowhat you want
}
}
in "Cell[0]" the zero index is the index of checkbox column

Assuming you only have one checkbox column in your grid, you can use the following code to access that value. If you have more than one column of that type, you might want to do some sort of name check or perhaps bind the grid to a model.
Note: Make sure to check for null values.
foreach (Telerik.WinControls.UI.GridViewRowInfo rowInfo in reportGridView.Rows)
{
foreach (Telerik.WinControls.UI.GridViewCellInfo cell in rowInfo.Cells)
{
if (cell.ColumnInfo.GetType() == typeof(Telerik.WinControls.UI.GridViewCheckBoxColumn))
{
if (cell.Value != null && (bool)cell.Value == true)
{
// Some logic here to handle this ...
}
}
}
}

Related

How to exclusively set the value of a DataGridViewCheckBoxCell?

I have a List<Car> objects that have a bool property named Marked.
I want to check / uncheck the Cell corresponding to this property, in a way that only one Car can be selected at the same time.
I want that the value of this property is updated in the bank.
Sample table:
Car Name
Marked
a
 
b
 
The problem is I can't check the state of CheckBox Cells.
This is what I tried, but it's not working:
Example 1:
DataGridViewCheckBoxCell dataGridViewCheckBoxCell = DataGridView1.Rows[e.RowIndex].Cells["Marked"] as DataGridViewCheckBoxCell;
if(Convert.ToBoolean(dataGridViewCheckBoxCell.Value) == true)
{
//some code
}
else if(Convert.ToBoolean(dataGridViewCheckBoxCell.Value) == false)
{
//some code
}
Example 2:
foreach (DataGridViewRow row in DataGridView1.Rows)
{
DataGridViewCheckBoxCell chk =(DataGridViewCheckBoxCell)row.Cells["Marked"];
if (chk.Value == chk.TrueValue)
{
chk.Value = chk.FalseValue;
}
else
{
chk.Value = chk.TrueValue;
}
}
how can I do it?
You can handle the CellContentClick (or CellClick) event of your DataGridView to set the state of a DataGridViewCheckBoxCell that should be a single choice (hence behaving as a RadioButton).
Store the Cell object currently selected when the Cell meets the criteria (its OwningColumn is a DataGridViewCheckBoxColumn named Marked).
Change the state of this Cell if it's the one currently selected, or reset it back to false if it's not.
In this case, set the current Cell to the one just selected and set its state to true.
Then you don't need to loop all the Rows each time a CheckBox changes value.
If you need to, call [DataGridView].EndEdit() to update the value immediately.
For example:
DataGridViewCheckBoxCell currentCheckBoxCell = null;
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
var dgv = sender as DataGridView;
if (dgv[e.ColumnIndex, e.RowIndex] is DataGridViewCheckBoxCell cell &&
cell.OwningColumn.Name == "Marked")
{
if (currentCheckBoxCell is null) currentCheckBoxCell = cell;
if (cell == currentCheckBoxCell) {
cell.Value = !(bool)cell.Value;
}
else {
currentCheckBoxCell.Value = false;
currentCheckBoxCell = cell;
}
// Affects CellValueChanged
// dgv.EndEdit();
}
}
This is how it works:
See also DataGridView CheckBox selection bug to select a CheckBox Cell by clicking anywhere in a Row and while immediately update the data source.
It is unclear “where” the first code snippet is called from so I will focus on the second snippet of code.
One of the problems you will have comes from the if statement…
if (chk.Value == chk.TrueValue) …
This condition chk.Value == chk.TrueValue may not throw an error, HOWEVER… this is checking two different OBJECTS… chk.Value is an OBJECT as is chk.TrueValue … so it makes sense that the two “different” OBJECTS would NEVER be equal.
One way to solve this is to simply “cast” those OBJECTS to bool values like…
if ((bool)chk.Value == (bool)chk.TrueValue) {…
HOWEVER the above line of code will ONLY work “IF”… the check box columns TrueValue AND FalseValue properties have been SET like…
CheckBoxColumn.TrueValue = true;
CheckBoxColumn.FalseValue = false;
IF you have NOT set the Check box column’s two properties TrueValue AND FalseValue like above… then the Check box columns TrueValue and FalseValue will be null and this will cause the line of code … if ((bool)chk.Value == (bool)chk.TrueValue) … to throw an exception since chk.TrueValue is null and the cast to a bool will throw a null exception.
Therefore, if you DO SET the Check box columns TrueValue and FalseValue then you will have to “cast” the objects to bool values in order for the comparison to work as shown above.
Given all this… you may want to re-consider using the Check box columns TrueValue and FalseValue as the intended purpose of those properties is to allow you to “change” the actual true and false values to something else that is not necessarily a true or false value.
It is unimportant how you would use these properties in that context as it appears very clear that in this context… setting the Check box columns TrueValue and FalseValue properties to true and false is somewhat superfluous and creates more work for you.
So in this case, I suggest you completely drop using the Check box columns TrueValue and FalseValue properties.
If you want to ensure that only ONE check box is checked in the column… then you could wire up the grids CellContentClick event. If the check box value changed, then simply “uncheck” all the cells in that column.
Fortunately, the grids CellContentClick will fire “before” it actually sets the check box cell, so unchecking them all works out fine for both checked and unchecked states. Crude yes, but it should work.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
if (dataGridView1.Columns[e.ColumnIndex].Name == "Marked") {
foreach (DataGridViewRow row in dataGridView1.Rows) {
if (!row.IsNewRow) {
row.Cells["Marked"].Value = false;
}
}
}
}
I hope this makes sense and helps.

How to checked row checked Janus Grid C#

I use janus gridex datagridview, In first column type is CheckBocx, I want to check if current row selected is checked or not, but my code not working
if(DBGrid.CurrentRow.Cells["CheckBoxColumn"].Value == true)
{
// do something
}
If the row is 'ActAsSelector = True' then you can use the row level checking itself :
if(DBGrid.CurrentRow.IsChecked)
{
// do something
}
Otherwise you need to cast the value to Boolean.
if((bool)DBGrid.CurrentRow.Cells["CheckBoxColumn"].Value == true)
{
// do something
}
If the row is 'ActAsSelector = True', you can use itself :
var CheckedList = string.Join(",",
gridEX1.GetCheckedRows()
.Cast<Janus.Windows.GridEX.GridEXRow>()
.Select(x => x.Cells["ID"].Text)
.ToArray());

Sorting by clicking column returns back all columns in C#

I have a gridview table that has auto generated columns and the rows in those columns can be sorted by clicking the column name since I have "AllowSorting" on. I also have checkbox lists that the user can check or uncheck to filter columns in and out of the table.
The problem is when the user filters the table to whatever columns they want and then they click on any column name, it will sort by that column but it brings back all of the columns that they filtered out.
I am guessing that I need to create a class with GridViewSortEventArgs and make the event grab only the columns the user filters instead of selecting all the columns in the database. I tried this but I don't know if I'm headed in the right direction or not but I also do get an error on GetSortColumnIndex() saying "not all code paths return a value". SQLQueryBuilder() obviously builds the query for the table btw.
private void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
int sortColumnIndex = GetSortColumnIndex();
if (sortColumnIndex != -1)
SQLQueryBuilder();
}
int GetSortColumnIndex()
{
foreach (DataControlField field in GridView1.Columns)
{
if (field.SortExpression == GridView1.SortExpression)
{
return GridView1.Columns.IndexOf(field);
}
}
}
not all code paths return a value
int GetSortColumnIndex()
{
foreach (DataControlField field in GridView1.Columns)
{
if (field.SortExpression == GridView1.SortExpression)
{
return GridView1.Columns.IndexOf(field);
}
}
}
You only return a value if field.SortExpression == GridView1.SortExpression, so add a return -1; at the end of your method.
As for the filtered values problem, do you account for the filtering in
SQLQueryBuilder();
My guess is you're rebuilding the query from scratch there.

How to check only the enabled checkboxes in a datagrid using checkboxheader in c#?

I have a Datagrid, named ShowGrid. The first column of the Datagrid is a Checkbox column and I have a CheckboxHeader for Checking/Unchecking all the rows in the Datagrid.
I can also select particular row checkbox for a Buttonclick.After the Buttonclick, I disable the particular rows's Checkbox.
After this, if I check the checkboxHeader, the disabled checkbox is also getting checked. So how can I select only the enabled checkboxes on checking on CheckboxHeader. I have used the following code, But I get Object reference not set to an instance of an object. exception.
foreach (DataGridViewRow row in ShowGrid.Rows)
{
if ((bool)row.Cells[0].Value == true && (bool)row.Cells[0].ReadOnly == false)
{
ShowGrid[0, row.Index].Value = ((CheckBox)ShowGrid.Controls.Find("checkboxHeader", true)[0]).Checked;
}
}
ShowGrid.EndEdit();
Update your if statement code as following
if (row.Cells != null &&(bool)row.Cells[0].Value == true && (bool)row.Cells[0].ReadOnly == false)
{
ShowGrid[0, row.Index].Value = ((CheckBox)ShowGrid.Controls.Find("checkboxHeader", true)[0]).Checked;
}

How to get IDs of only checked rows of a datagridview

I have a datagridview that contains list of subjects populated from Subject table from database.Columns include
Select(checkbox),
SubjectId,
SubjectName,
SubjectGroup.
Now I want if a user Selects on any of the desired rows, the corresponding SubjectId's should be added to a List. I have made and inserted into the desired table in the database.
The problem is that the new column of checkboxes I have added to this datagridview is not being detected.
My code is:
foreach (DataGridViewRow row in gvSubjectsOpted.Rows)
{
if (Convert.ToBoolean(gvSubjectsOpted.SelectedRows[0].Cells["SelectId"].Value=true))
{
olist.Add(gvSubjectsOpted.SelectedRows[0].Cells["SubjectId"].Value.ToString());
}
}
Late to the party. I had the same issue with trying to get the checkbox column by name, use the index instead. Here is a linq example assuming the checkbox is column 0 and the stored values for TrueValue and FalseVale are true and false respectively.
var checkedRows = from DataGridViewRow r in gvSubjectsOpted.Rows
where Convert.ToBoolean(r.Cells[0].Value) == true
select r;
foreach (var row in checkedRows)
{
olist.Add(row.Cells["SubjectId"].Value.ToString());
}
I realise this is an old post but I came across it and didn't think it was really answered in an efficient way so I thought I would add my method.
I have a similar block in my windows app. I read the values from the grid when the user clicks a button, and I want to know which rows they checked. As the checkboxes are in Cell 0 and the data I want is in Cell 1, I use the following code. Note the cast: it is important as it allows us the use the Where clause and therefore just a single line of code to get the collection of data. I could use the name of the cells instead of magic index numbers but then it would not fit your app so I put numbers instead (you should use names)
var checkedRows = dataGridView
.Rows
.Cast<DataGridViewRow>()
.Where(x => x.Cells[0].Value.ToString() == "1")
.Select(x => x.Cells[1]);
Note that this will give you an IEnumerable of type DataGridViewCell. If you want you can either add something like .Value.ToString() to the select or do this when you use your collection.
You question is similar to another SO question.
Check the answer of this Datagridview checkboxcolumn value and functionality.
Try this
foreach(GridViewRow r in gvSubjectsOpted.Rows)
{
GridViewCheckBoxColumn c = r.cells[0].Controls[0] as GridViewCheckBoxColumn;
if(c.Checked)
{
//Do something.
}
}
private void button1_Click(object sender, EventArgs e)
{
string subjId;
List<string> lines = new List<string>();
for (int i = 0; i < gvSubjectsList.Rows.Count; i++)
{
bool Ischecked =Convert.ToBoolean(gvSubjectsList.Rows[i].Cells["Select"].Value);
if (Ischecked == true)
{
subjId = gvSubjectsList.Rows[i].Cells["SubjectId"].Value.ToString();
lines.Add(subjId);
}
}
comboBox1.DataSource = lines;
}
//the most important thing is to set 'true' and 'false' values against newly added checkboxcolumn instead of '0' and '1'...that is,
CBColumn.FalseValue = "false";
CBColumn.TrueValue = "true";

Categories

Resources