I have a RadGridView with GridViewCheckboxColumn.
When I click on any cell in the grid I catch the CellClick event of the grid and check/uncheck the checkboxcolumn in the specific current row.
It work fine in all the cells except for click on the checkbox cell that do change the value but the result doesn’t appear in the checkbox.
The changes are seen only after I move the cursor.
How it is possible that the value is changed but the checkbox isn’t checked/ unchecked ?
void RadGridView_CellClick(object sender, GridViewCellEventArgs e)
{
if (e.RowIndex > -1)
{
if (e.Row.Cells["Sel"] != null)
{
if (e.Row.Cells["Sel"].Value != DBNull.Value)
e.Row.Cells["Sel"].Value = !Convert.ToBoolean(e.Row.Cells["Sel"].Value);
else
e.Row.Cells["Sel"].Value = true;
}
}
}
34 Answers, 1 is accepted
Hello Dror,
to make the changes visible you should call this after changing the checkbox values:
RadGridView1.TableElement.Update(GridUINotifyAction.DataChanged)
Thank you for your quick replay.
I tried your solution but it didn’t worked.
There is no actually property as TableElement for RadGridView, instead I tried to use the GridElement as follow:
RadGridView1.GridElement.Update(GridUINotifyAction.DataChanged)
but it didn’t work.
Did I missed anything?
RadGridView1.TableElement.Update(GridUINotifyAction.Reset)
i use RadControls for WinForms Q1 2010 SP2.
This is the link to download q2 ?
http://www.telerik.com/account/free-trials/download-trial-file.aspx?pid=523
How do i know about th new SP ?
Is your grid readonly or not? If not then you might set readonly for the checkbox column, because otherwise when you click on the checkbox in the column it will change the value once for the click and again for the cell click...
Hope this helps,
Emanuel Varga
i download the setup from http://www.telerik.com/account/free-trials/download-trial-file.aspx?pid=523
but when i try to install it i get an error "...is not a valid win 32 applictaion"
i use win xp.
Dror, we would recommend that you try our latest release (Q2 2010). We will appreciate your feedback and will help with the transition process.
All the best,
Jack
the Telerik team
Basically the radGridView_CellClick event will fire when I click on any other cell. However when i click on a cell containing a checkbox. It will add the check to the box, but the event does not fire.
Sorry it took this long to get back to you but i've prepared something that will work, you should handle the on MouseDown event of the grid and it looks something like this: (Sorry i don't have time to clean it up more)
private
void
radGridView1_MouseDown(
object
sender, MouseEventArgs e)
{
var dataCell =
this
.radGridView1.ElementTree.GetElementAtPoint(e.Location)
as
GridDataCellElement;
var checkBoxCell =
this
.radGridView1.ElementTree.GetElementAtPoint(e.Location)
as
RadCheckBoxEditorElement;
if
(dataCell !=
null
|| checkBoxCell !=
null
)
{
if
(dataCell !=
null
)
{
try
{
var value = dataCell.RowInfo.Cells[
"Checked"
].Value !=
null
? !(
bool
)dataCell.RowInfo.Cells[
"Checked"
].Value :
true
;
dataCell.RowInfo.Cells[
"Checked"
].Value = value;
}
catch
(Exception)
{
}
}
else
{
switch
(checkBoxCell.CheckState)
{
case
Telerik.WinControls.Enumerations.ToggleState.Off:
checkBoxCell.CheckState = Telerik.WinControls.Enumerations.ToggleState.On;
break
;
case
Telerik.WinControls.Enumerations.ToggleState.On:
checkBoxCell.CheckState = Telerik.WinControls.Enumerations.ToggleState.Off;
break
;
default
:
break
;
}
}
}
}
Please let me know if this works for you,
Best regards,
Emanuel Varga
Fix: Sorry for before i missed the fact that the first time you click it's unchecking and checking again the checkbox, but it works great if you use the MouseUp event.
Best regards,
Emanuel Varga
"dataCell" and "checkBoxCell" are null and therefore nothing happens.
If you click directly into the checkbox this should work as default if you don't have the grid set to readonly, if you have it on readonly you should check also for the checkMark click, using the following:
(I've updated the code for this)
void
radGridView1_MouseUp(
object
sender, MouseEventArgs e)
{
var dataCell =
this
.radGridView1.ElementTree.GetElementAtPoint(e.Location)
as
GridDataCellElement;
var checkBoxCell =
this
.radGridView1.ElementTree.GetElementAtPoint(e.Location)
as
RadCheckBoxEditorElement;
var checkMark =
this
.radGridView1.ElementTree.GetElementAtPoint(e.Location)
as
RadCheckmark;
if
(dataCell !=
null
|| checkBoxCell !=
null
|| checkMark !=
null
)
{
if
(dataCell !=
null
)
{
try
{
var value = dataCell.RowInfo.Cells[
"Checked"
].Value !=
null
? !(
bool
)dataCell.RowInfo.Cells[
"Checked"
].Value :
true
;
dataCell.RowInfo.Cells[
"Checked"
].Value = value;
}
catch
(Exception)
{
}
}
else
if
(checkBoxCell !=
null
)
{
switch
(checkBoxCell.CheckState)
{
case
Telerik.WinControls.Enumerations.ToggleState.Off:
checkBoxCell.CheckState = Telerik.WinControls.Enumerations.ToggleState.On;
break
;
case
Telerik.WinControls.Enumerations.ToggleState.On:
checkBoxCell.CheckState = Telerik.WinControls.Enumerations.ToggleState.Off;
break
;
}
}
else
{
switch
(checkMark.CheckState)
{
case
Telerik.WinControls.Enumerations.ToggleState.Off:
checkMark.CheckState = Telerik.WinControls.Enumerations.ToggleState.On;
break
;
case
Telerik.WinControls.Enumerations.ToggleState.On:
checkMark.CheckState = Telerik.WinControls.Enumerations.ToggleState.Off;
break
;
}
}
}
}
Best Regards,
Emanuel Varga
for instance, if the box is checked and you click on the box, when the event fires, the checkMark.CheckState = true. Even though it should remove the check. So the next time the box is clicked, the checkMark.CheckState still = true. The checked value never changes. If you click the area in the cell outside of the checkbox, it works as expected. But thats not exactly reasonable. I can add code to manually remove/add the checkmark to the box, but shouldn't that already be taken care of?
Thanks!
also, the cell on the gridview is set to ReadOnly = false.
gvMassTickets.MasterTemplate.Columns[0].ReadOnly =
false
;
gvMassTickets.MasterTemplate.Columns[1].ReadOnly =
true
;
gvMassTickets.MasterTemplate.Columns[2].ReadOnly =
true
;
gvMassTickets.MasterTemplate.Columns[3].ReadOnly =
true
;
however when i deleted that code and just set the entire grid to ReadOnly = true. It works.
Thanks.
If readonly is set to false then my first solution should have worked (with the MouseUp event) because it's normal if the check box is enabled for it to catch the click and change it's state and after that the row event comes and changes it again or the other way around (that's why i ignored this case in the first version),
please try that one again on a new very small project and you will see it works, sadly i cannot send you my test project on the forums but it has to work like this.
void
radGridView1_MouseClick(
object
sender, MouseEventArgs e)
{
RadElement element =
this
.radGridView1.ElementTree.GetElementAtPoint(e.Location);
if
(element
is
RadCheckBoxEditorElement)
{
GridCheckBoxCellElement checkboxCell = (GridCheckBoxCellElement)element.Parent;
//...
}
}
I hope this helps.
Jack
the Telerik team
private
void
RgList_CellClick(
object
sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
GridHeaderCellElement headerelement = sender
as
GridHeaderCellElement;
if
(headerelement ==
null
)
{
bool
flag = RgList.MasterTemplate.Rows[e.RowIndex].Cells[
"Chk1"
].IsSelected;
RgList.MasterTemplate.Rows[e.RowIndex].Cells[
"Chk1"
].IsSelected = !flag;
RgList.MasterTemplate.Rows[e.RowIndex].Cells[
"Chk1"
].Value = !flag;
}
}
Regards.
Kind regards,
Jack
the Telerik team
Basically the radGridView_CellClick event will fire when I click on any other cell. However when i click on a cell containing a checkbox. It will add the check to the box, but the event does not fire.
reg:Validation of check box control in gridview.
I am using GridviewCheckbox column inside the telerik grid.When I am clicking inside the check box column the cell click event is not firing,for another columns its firing.
my concern is,
example:->while user selecting the check box inside the grid
If(textbox1.text=="")
{
messagebox.show("check box selection is not allowed");
}
and the old value of check box should be retained.Is there anyway to do this.
thanks in advance
Kannan
Thank you for writing.
There is not a straight-forward way to implement your requirement. However, one approach is to make your check-box column read only in order to retain its values and show a message box on mouse click.
void
radGridView1_MouseClick(
object
sender, MouseEventArgs e)
{
RadCheckmark checkmark =
this
.radGridView1.ElementTree.GetElementAtPoint(e.Location)
as
RadCheckmark;
if
(checkmark !=
null
)
{
MessageBox.Show(
"CheckBox cannot be checked/unchecked"
);
}
}
Hope this helps. Let me know if you have further questions.
Kind regards,
Martin Vasilev
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
*I am using Telerik:RadGrid
When the data type is string or Float the value should be in text, if Data type is Boolean, value should be in Checkbox. When It is Enum, value should be a comboBox. Please find the attached "requirment.png" for more detail.
Please let me know, How do I achieve this ?
*I am using Telerik:RadGrid
The desired scenario is supported by the latest version of RadGridView out of the box. You should ensure that the AutoGenerateColumns property is set to true.
Regards,
Svett
the Telerik team
private
void
dataGridViewAdresse_MouseUp(
object
sender, MouseEventArgs e)
{
Telerik.WinControls.UI.GridHeaderCellElement headerelement = sender
as
Telerik.WinControls.UI.GridHeaderCellElement;
if
(headerelement ==
null
)
{
if
(
bool
.Parse(dataGridViewAdresse.CurrentRow.Cells[
"03_AdrPpl"
].Value.ToString()) ==
false
)
{
for
(
int
i = 0; i < dataGridViewAdresse.RowCount; i++)
{
if
(i != dataGridViewAdresse.CurrentRow.Index)
{
if
(
bool
.Parse(dataGridViewAdresse.ChildRows[i].Cells[
"03_AdrPpl"
].Value.ToString()) ==
true
)
{
dataGridViewAdresse.ChildRows[i].Cells[
"03_AdrPpl"
].Value =
"False"
;
}
}
}
}
}
}
I use a radgridview component with the first column in it of type "GridViewCheckBoxColumn".
I also have a label in which I have to show the number of rows checked.
The problem is - the number of rows checked always lags behind (one or sometimes even two steps behind).
For example: I load data into the radgrid and initially the checkbox cells are all not checked. Then I click on one of the checkbox cells, it gets checked, but the number in the label does not update at once as it should.
I tried using various events (MouseClick/MouseDown/MouseUp, ValueChanged/ValueChanging, Validate/Validating, CellClick).
I also tried writing the following lines within the body of the event:
RadGridView1.TableElement.Update(GridUINotifyAction.Reset) ;
RadGridView1.EndEdit()";
and also these:
RadGridView1.TableElement.Update(GridUINotifyAction.DataChanged) ;
RadGridView1.EndEdit() ;
All these don't help. The number of rows checked still doesn't update immediately after the change of the state of the checkbox cell.
Is there a way to do it right after the checkbox cell click?
P.S.: I have not found an identical topic on the forum, so I wrote here. If this issue has already been solved elsewhere on the forum, please excuse me and redirect me.
Thank you in advance.
Best Regards,
Jeen.
Thank you for writing.
When you click a cell which belongs to GridViewCheckBoxColumn, the cell enters edit mode. Although, the check box state is immediately changed as it is a permanent editor the cell's value is still not updated until the editor's value is committed by pressing Enter key for example. However, you can detect when the RadCheckBoxEditor.Value is changed using its ValueChanged event. In the event handler you can iterate through all the rows and take the value in the check box cell, except for the currently updating cell. For the editing cell, we will take the value from the editor.
I would like to note again that the editor's value is not stored in the cell's value yet as the grid is in edit mode. The user is allowed either to accept the changes by pressing Enter key, or cancel the changes by pressing Escape key. Here is a sample code snippet:
public
Form1()
{
InitializeComponent();
DataTable dt =
new
DataTable();
dt.Columns.Add(
"Id"
,
typeof
(
int
));
dt.Columns.Add(
"Name"
,
typeof
(
string
));
dt.Columns.Add(
"IsActive"
,
typeof
(
bool
));
for
(
int
i = 0; i < 20; i++)
{
dt.Rows.Add(i,
"Name"
+ i,
false
);
}
this
.radGridView1.DataSource = dt;
this
.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
}
private
void
radGridView1_CellEditorInitialized(
object
sender, GridViewCellEventArgs e)
{
RadCheckBoxEditor cbEditor = e.ActiveEditor
as
RadCheckBoxEditor;
if
(cbEditor !=
null
)
{
cbEditor.ValueChanged -= cbEditor_ValueChanged;
cbEditor.ValueChanged += cbEditor_ValueChanged;
}
}
private
void
cbEditor_ValueChanged(
object
sender, EventArgs e)
{
int
checkedRows = 0;
foreach
(GridViewRowInfo row
in
radGridView1.Rows)
{
if
((
bool
)row.Cells[
"IsActive"
].Value ==
true
&& row !=
this
.radGridView1.CurrentRow)
{
checkedRows++;
}
}
RadCheckBoxEditor cbEditor = sender
as
RadCheckBoxEditor;
if
((ToggleState)cbEditor.Value == ToggleState.On)
{
checkedRows++;
}
this
.radLabel1.Text =
"Checked Rows: "
+ checkedRows;
}
I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Desislava
Telerik
Excuse me, please, for the delay to answer.
Thank you very much for your help and the sample project!! I made a slight modification to your code, and now it works great for me!!)
However, I have one more question.
The logics has it that everytime I click on one of the previously checked cells to uncheck it, the number in the label text should diminish by one. I have tried to modify you code to achieve this, but so far with no success.
Would you please give me a hint on this?
Thanks in advance.
Best Regards,
Jeen.
I have already solved the problem with unchecking the previously checked checkbox cells by modifying the code provided by Desislava.
Here is the code snippet modfied:
...
RadCheckBoxEditor cbEditor = sender as RadCheckBoxEditor;
if ((ToggleState)cbEditor.Value == ToggleState.On)
{
checkedRows++;
}
else
{
if (checkedRows > 0)
checkedRows --;
}
this.radLabel1.Text = "Checked Rows: " + checkedRows;
}
...
The only small issue that remains is : when I check cells from the first down to the last, the counter in the label goes: "0 -> 1 -> 2 -> 3 -> ....." (which is OK). Then I uncheck them from the last up to the first, and the counter goes: ".... -> 3 -> 2 -> 1 -> 0" (also OK). And then I check the first one again, but the counter goes: "0 -> 2 -> 2 -> 3 -> ...." (which is NOT ok).
Would you be so kind as to hint me on how to solve this, please.
Thanks a lot in advance.
Best Regards,
Jeen
Thank you for writing back.
Following the provided code snippet, I modified my sample project. However, I was unable to reproduce the issue you are facing with the latest version Q2 2014 SP1. Here is the code example:
public
Form1()
{
InitializeComponent();
DataTable dt =
new
DataTable();
dt.Columns.Add(
"Id"
,
typeof
(
int
));
dt.Columns.Add(
"Name"
,
typeof
(
string
));
dt.Columns.Add(
"IsActive"
,
typeof
(
bool
));
for
(
int
i = 0; i < 10; i++)
{
dt.Rows.Add(i,
"Name"
+ i,
false
);
}
this
.radGridView1.DataSource = dt;
this
.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
}
private
void
radGridView1_CellEditorInitialized(
object
sender, GridViewCellEventArgs e)
{
RadCheckBoxEditor cbEditor = e.ActiveEditor
as
RadCheckBoxEditor;
if
(cbEditor !=
null
)
{
cbEditor.ValueChanged -= cbEditor_ValueChanged;
cbEditor.ValueChanged += cbEditor_ValueChanged;
}
}
int
checkedRows = 0;
private
void
cbEditor_ValueChanged(
object
sender, EventArgs e)
{
RadCheckBoxEditor cbEditor = sender
as
RadCheckBoxEditor;
if
((ToggleState)cbEditor.Value == ToggleState.On)
{
checkedRows++;
}
else
{
if
(checkedRows > 0)
checkedRows --;
}
this
.radLabel1.Text =
"Checked Rows: "
+ checkedRows;
}
The attached gif file illustrates better the obtained behavior. Everything works as expected on my end. Could you please specify the exact steps how to reproduce the problem or get back to me with a sample code snippet, reproducing the issue so I can investigate the precise case? Thank you in advance.
I am looking forward to your reply.
Regards,
Desislava
Telerik
Thank you for your answer!
In your gif-file it really works as it should.
In the meantime I have managed to solve the problem in another way, by making another modification and a transposition of a line in the sample code snippet from your previous post.
Below is the code extract from my project:
private void cbEditor_ValueChanged(object sender, EventArgs e)
{
int counter = 0;
foreach(GridViewRowInfo row in MyRadGrid.Rows)
{
if ((bool)row.Cells["ColumnName"].Value == true)
counter++;
}
//As you have probably noticed, I have moved this line three lines up, since it didn't work well for me from where it was before
RadCheckBoxEditor cbEditor = sender as RadCheckBoxEditor;
//
if(counter == MyRadGrid.Rows.Count)
checkedRows == MyRadGrid.Rows.Count;
if ((ToggleState)cbEditor.Value == ToggleState.On)
{
checkedRows++;
}
else
{
if (checkedRows > 0)
checkedRows --;
}
this.MyLabel1.Text = "Checked Rows: " + checkedRows;
}
Thanks a lot for your help!
My problem is finally solved :)
Best regards,
Jeen
Thank you for writing back.
I am glad that you have found a suitable solution for the specific problem. However, you can find attached my sample project, which behavior is illustrated in the gif file from my previous post.
If you have any additional questions, please let me know.
Regards,
Desislava
Telerik
Sorry for the delay with the answer (just returned from my vacation).
Thank you for the sample project zip-file.
It's always good to know different ways to solve a problem.
Best regards,
Jeen