Hello
i want to select Gridview rows by pressing Space Key, and change the status of them to selected. for example by pressing Space , if the row is selected change the status of it to no selected ,if its not selected change the status to selected.in addition i want to change the back color of the selected row.
Ill be thankfull if you could help me.
i want to select Gridview rows by pressing Space Key, and change the status of them to selected. for example by pressing Space , if the row is selected change the status of it to no selected ,if its not selected change the status to selected.in addition i want to change the back color of the selected row.
Ill be thankfull if you could help me.
9 Answers, 1 is accepted
0
Richard Slade
Top achievements
Rank 2
answered on 02 May 2012, 11:20 AM
Hello Roya,
Here is an example of what you have asked for. The RadGridView in this example is ReadOnly as the Space key usually trigger a cell edit.
As for changing the back colour, you can do this via the RowFormatting event
For example
You can read more about formatting rows and cells here: (formatting rows & formatting cells)
Hope this helps
Richard
Here is an example of what you have asked for. The RadGridView in this example is ReadOnly as the Space key usually trigger a cell edit.
// keep track of the selected index
private
int
_selectedRowIndex = 0;
this
.radGridView1.SelectionChanged +=
new
EventHandler(radGridView1_SelectionChanged);
this
.radGridView1.KeyDown +=
new
KeyEventHandler(radGridView1_KeyDown);
this
.radGridView1.ReadOnly =
true
;
this
.radGridView1.MultiSelect =
false
;
this
.radGridView1.Rows[0].IsCurrent =
true
;
this
.radGridView1.Rows[0].IsSelected =
true
;
void
radGridView1_SelectionChanged(
object
sender, EventArgs e)
{
if
(
this
.radGridView1.SelectedRows.Count > 0)
{
_selectedRowIndex =
this
.radGridView1.SelectedRows[0].Index;
}
}
void
radGridView1_KeyDown(
object
sender, KeyEventArgs e)
{
if
(e.KeyCode == Keys.Space)
{
if
(
this
.radGridView1.SelectedRows.Count > 0)
{
_selectedRowIndex =
this
.radGridView1.SelectedRows[0].Index;
this
.radGridView1.ClearSelection();
this
.radGridView1.CurrentRow =
null
;
}
else
{
this
.radGridView1.Rows[_selectedRowIndex].IsCurrent =
true
;
this
.radGridView1.Rows[_selectedRowIndex].IsSelected =
true
;
}
}
}
As for changing the back colour, you can do this via the RowFormatting event
For example
this
.radGridView1.RowFormatting +=
new
RowFormattingEventHandler(radGridView1_RowFormatting);
void
radGridView1_RowFormatting(
object
sender, RowFormattingEventArgs e)
{
if
(e.RowElement.IsSelected && e.RowElement.IsCurrent)
{
e.RowElement.NumberOfColors = 1;
e.RowElement.BackColor = System.Drawing.Color.LightBlue;
e.RowElement.DrawFill =
true
;
}
else
{
e.RowElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
e.RowElement.ResetValue(LightVisualElement.BackColorProperty);
e.RowElement.ResetValue(LightVisualElement.DrawFillProperty);
}
}
You can read more about formatting rows and cells here: (formatting rows & formatting cells)
Hope this helps
Richard
0
Roya
Top achievements
Rank 1
answered on 02 May 2012, 01:47 PM
Thank you Richard
I Used Your solution and it works, but I want to have multiselection .is it possible?
I Used Your solution and it works, but I want to have multiselection .is it possible?
0
Richard Slade
Top achievements
Rank 2
answered on 02 May 2012, 02:00 PM
Hello Roya,
You should just be able to replace the reference to _selectedRowIndex to an Integer array. You can then cycle through the array in the key down to select the rows that were previously selected.
If this helps, please remember to mark all helpful posts as answer. If you need further assistance, just let me know
thanks
Richard
You should just be able to replace the reference to _selectedRowIndex to an Integer array. You can then cycle through the array in the key down to select the rows that were previously selected.
If this helps, please remember to mark all helpful posts as answer. If you need further assistance, just let me know
thanks
Richard
0
Roya
Top achievements
Rank 1
answered on 02 May 2012, 02:39 PM
Thank you Richard
is it possible for you to tell me how can i do it.its not clear for me
is it possible for you to tell me how can i do it.its not clear for me
0
Richard Slade
Top achievements
Rank 2
answered on 02 May 2012, 03:26 PM
Hello Roya,
Here is a sample for you. Hope it helps. Please remember to mark as answer if this helps or let me know if you have further questions
Designer File
Form
Regards,
Richard
Here is a sample for you. Hope it helps. Please remember to mark as answer if this helps or let me know if you have further questions
Designer File
namespace
RadControlsWinFormsApp1
{
partial
class
RadForm1
{
/// <summary>
/// Required designer variable.
/// </summary>
private
System.ComponentModel.IContainer components =
null
;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected
override
void
Dispose(
bool
disposing)
{
if
(disposing && (components !=
null
))
{
components.Dispose();
}
base
.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private
void
InitializeComponent()
{
this
.radGridView1 =
new
Telerik.WinControls.UI.RadGridView();
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(
this
)).BeginInit();
this
.SuspendLayout();
//
// radGridView1
//
this
.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this
.radGridView1.Location =
new
System.Drawing.Point(0, 0);
this
.radGridView1.Name =
"radGridView1"
;
this
.radGridView1.Size =
new
System.Drawing.Size(343, 467);
this
.radGridView1.TabIndex = 4;
this
.radGridView1.Text =
"radGridView1"
;
//
// RadForm1
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(343, 467);
this
.Controls.Add(
this
.radGridView1);
this
.Name =
"RadForm1"
;
//
//
//
this
.RootElement.ApplyShapeToControl =
true
;
this
.Text =
"RadForm1"
;
this
.ThemeName =
"ControlDefault"
;
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(
this
)).EndInit();
this
.ResumeLayout(
false
);
}
#endregion
private
Telerik.WinControls.UI.RadGridView radGridView1;
}
}
Form
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
Telerik.WinControls;
using
Telerik.WinControls.UI;
using
Telerik.WinControls.Primitives;
using
System.Linq;
namespace
RadControlsWinFormsApp1
{
public
partial
class
RadForm1 : Telerik.WinControls.UI.RadForm
{
private
GridViewRowInfo[] _selectedRows;
private
int
_currentRow = 0;
public
RadForm1()
{
InitializeComponent();
List<User> users =
new
List<User>();
int
k = 0;
while
(k <= 20)
{
users.Add(
new
User(k,
"User "
+ k.ToString(), k));
k++;
}
this
.radGridView1.DataSource = users;
this
.radGridView1.KeyDown +=
new
KeyEventHandler(radGridView1_KeyDown);
this
.radGridView1.ReadOnly =
true
;
this
.radGridView1.MultiSelect =
true
;
this
.radGridView1.Rows[0].IsCurrent =
true
;
this
.radGridView1.Rows[0].IsSelected =
true
;
_selectedRows =
new
GridViewRowInfo[0];
this
.radGridView1.RowFormatting +=
new
RowFormattingEventHandler(radGridView1_RowFormatting);
}
void
radGridView1_RowFormatting(
object
sender, RowFormattingEventArgs e)
{
if
(e.RowElement.IsSelected)
{
e.RowElement.NumberOfColors = 1;
e.RowElement.BackColor = System.Drawing.Color.LightBlue;
e.RowElement.DrawFill =
true
;
}
else
{
e.RowElement.ResetValue(LightVisualElement.NumberOfColorsProperty);
e.RowElement.ResetValue(LightVisualElement.BackColorProperty);
e.RowElement.ResetValue(LightVisualElement.DrawFillProperty);
}
}
void
radGridView1_KeyDown(
object
sender, KeyEventArgs e)
{
if
(e.KeyCode == Keys.Space)
{
if
(
this
.radGridView1.SelectedRows.Count > 0)
{
_selectedRows =
new
GridViewRowInfo[
this
.radGridView1.SelectedRows.Count];
_currentRow =
this
.radGridView1.CurrentRow.Index;
this
.radGridView1.SelectedRows.CopyTo(_selectedRows, 0);
this
.radGridView1.ClearSelection();
this
.radGridView1.CurrentRow =
null
;
}
else
{
this
.radGridView1.CurrentRow =
this
.radGridView1.Rows[_currentRow];
foreach
(GridViewRowInfo row
in
_selectedRows)
{
row.IsSelected =
true
;
row.InvalidateRow();
}
}
}
}
}
public
class
User
{
public
User(
int
id,
string
name,
int
jobId)
{
Id = id;
Name = name;
JobId = jobId;
}
public
User()
{ }
public
int
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
public
int
JobId
{
get
;
set
;
}
}
public
class
Job
{
public
Job(
int
id,
string
name)
{
Id = id;
Name = name;
}
public
Job()
{ }
public
int
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
}
}
Regards,
Richard
0
Roya
Top achievements
Rank 1
answered on 03 May 2012, 06:51 AM
Hello Richard
thank you Richard ,you reply my question very soon.
But i want to use Space to select rows,for example i want to select 10 rows then i press space on 10 rows one by one and all of this 10 rows will be selected and the back color of them will be change.then i press space on some of those 10 rows again so the status of them will be change to not selected and the back color of them will be change to default.
thank you Richard ,you reply my question very soon.
But i want to use Space to select rows,for example i want to select 10 rows then i press space on 10 rows one by one and all of this 10 rows will be selected and the back color of them will be change.then i press space on some of those 10 rows again so the status of them will be change to not selected and the back color of them will be change to default.
0
Accepted
Hello,
@Roya, the desired functionality will be a great addition to RadGridView , so I will add it in PITS for future implementation. I have updated your Telerik points for bringing this to our attention. Here is a link to the feature request: http://www.telerik.com/support/pits.aspx#/public/winforms/10983.
Attached, you can find the modified version of Richard's project, where the rows will be selected/deselected by pressing space. I am using the Tag property of the row to save its state. Additionally, all selected rows can be accessed at any time in the selectedRows List.
I hope this helps.
Regards,@Roya, the desired functionality will be a great addition to RadGridView , so I will add it in PITS for future implementation. I have updated your Telerik points for bringing this to our attention. Here is a link to the feature request: http://www.telerik.com/support/pits.aspx#/public/winforms/10983.
Attached, you can find the modified version of Richard's project, where the rows will be selected/deselected by pressing space. I am using the Tag property of the row to save its state. Additionally, all selected rows can be accessed at any time in the selectedRows List.
I hope this helps.
Stefan
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Roya
Top achievements
Rank 1
answered on 05 May 2012, 05:06 AM
Hello Stefan
Thank you very much, it was the answer.
Thank you very much, it was the answer.
0
I am glad that I could help. Should you have any other questions, do not hesitate to open a new thread.
All the best,Stefan
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>