every this is alright except one issue,
pleas have a look on my code
dataGridView1.DataSource = tab; //where tab is returned from the database and have more than 100 row
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
try
{
dataGridView1.Rows[i].Cells[0].CellElement.DrawFill = true;
dataGridView1.Rows[i].Cells[0].CellElement.BackColor = Color.Red;
}
catch
{ }
}
the loop stop every time on row number 9
it throw exception "Object reference not set to an instance of an object" on CellElement (CellElement is null)
is that because its a trial version?
i don't want to use Cell Formatting Event cuz this will not be helpful for me
thanks
9 Answers, 1 is accepted
Hi Mohammed
You used Worng Code
Check is Code
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) |
{ |
try |
{ |
dataGridView1.Rows[i].Cells[0].CellElement.DrawFill = true; |
dataGridView1.Rows[i].Cells[0].CellElement.BackColor = Color.Red; |
} |
catch |
{ } |
} |
foreach (Telerik.WinControls.UI.GridViewDataRowInfo Row in dataGridView1.Rows) |
{ |
try |
{ |
Row.Cells[0].CellElement.DrawFill = true; |
Row.Cells[0].CellElement.BackColor = Color.Red; |
} |
catch |
{ } |
} |
Hi HamiD
thanks for your reply
i have tried your code but still the same problem
what we want to do is
1-change the vell of a certain cell (done without problems)
2-color this cell with specified color for 1 second and then return the cell color to it's original color ,and the reset color method will be as for or foreach loop on all cells in datagrid to return them all to the original color ,and there is timer to run the rest method method
so the problem appear in step no 2 as sometimes the cell element is null when we try to change the cell color or retrieve it back to its original color
RadGridView uses virtualization for its elements. It creates visual elements only for the rows and cells that are currently visible. When scrolling or sorting they are reused. You can't access a cell element directly. You should handle CellFormatting event instead. This article in our online documentation describes how to access and customize grid cells. More information is available in this KB article. Should you have any further questions, don't hesitate to ask.
Sincerely yours,
Jack
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
thank you for your reply.
i know that CellFormatting event is the best way to customize the appearance of any cell but in my situation i want to change the color of specific cell on my Client application based on updates coming from the server and change back the cell color to its initial state after tow seconds, i have done that so easily using .net DataGridView and .net Timer cuz i can access the cell color directly any time in .net DataGridView, How can i accomplish that senario in RadGridView.
thanks in advance.
I understand. Still you have to use CellFormatting event. However, you can cause this event to fire by calling Update method of GridElement. Here is a sample:
this
.radGridView1.GridElement.Update(GridUINotifyAction.StateChanged);
Regards,
Jack
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
thank you for your reply.
i have used the update method in RadGridView and it solves half of the problem, the other half is how to pass data to that method to change the cell color depend on that data, something like that
public void update_prices(PricesUpdates[] updates) |
{ |
if (updates.Length > 0) |
{ |
for (int i = 0; i < updates.Length; i++) |
{ |
double number; |
string oldValue = ultraGrid1.Rows.Single(e => e.Cells["Symbol"].Value.ToString() == updates[i].Stock && e.Cells["Market"].Value.ToString() == updates[i].Market).Cells[updates[i].Attribute].Value.ToString(); |
int index = -1; |
index = ultraGrid1.Rows.IndexOf(ultraGrid1.Rows.Single(e => e.Cells["Symbol"].Value.ToString() == updates[i].Stock && e.Cells["Market"].Value.ToString() == updates[i].Market)); |
ultraGrid1.Rows[index].Cells[updates[i].Attribute].Value = updates[i].Value; |
bool result = double.TryParse(oldValue, out number); |
if (result == true) |
{ |
if (double.Parse(ultraGrid1.Rows.Single(e => e.Cells["Symbol"].Value.ToString() == updates[i].Stock && e.Cells["Market"].Value.ToString() == updates[i].Market).Cells[updates[i].Attribute].Value.ToString()) < double.Parse(oldValue)) |
{ |
ultraGrid1.Rows[index].Cells[updates[i].Attribute].BackColor = Color.Red; |
} |
else |
{ |
ultraGrid1.Rows[index].Cells[updates[i].Attribute].BackColor = Color.Green; |
} |
} |
else |
{ |
ultraGrid1.Rows[index].Cells[updates[i].Attribute].BackColor = Color.Red; |
} |
} |
} |
} |
note that this data is not fixed it comes from the server periodically.
thank you
void
radGridView1_CellFormatting(
object
sender, CellFormattingEventArgs e)
{
GridViewDataColumn dataColumn = e.CellElement.ColumnInfo
as
GridViewDataColumn;
if
(dataColumn !=
null
&& dataColumn.UniqueName ==
"Symbol"
)
{
if
(SomeCondition(e.CellElement.RowInfo, dataColumn))
{
e.CellElement.BackColor = Color.Red;
}
else
{
e.CellElement.BackColor = Color.Green;
}
e.CellElement.DrawFill =
true
;
e.CellElement.GradientStyle = GradientStyles.Solid;
}
}
Calling the Update method causes all cells in RadGridView to be updated. You have only to handle CellFormatting event and process the cells that meet certain condition. I hope this helps.
Regards,
Jack
the Telerik team
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
Thank you for writing.
Please note that this forms concerns RadControls for WInForms and not RadControls for WPF. If you need assistance with our WPF products, please post your question in the appropriate forums.
Here is a link from the online documentation of RadControls for WPF, regarding the desired functionality: http://www.telerik.com/help/wpf/gridview-cell-style-selector.html.
If you have any additional questions, feel free to post them in the WPF forums.
Regards,
Stefan
the Telerik team
Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.