Monday, May 7, 2012

How to manipulate the gridview cell

Here i will show how to manipulate the grid view cell styles in windows forms application.For this i will get all grid view rows and compare the grid view header cell value with drop down selected value. Then the related data cells back colour will be changed to Green.In foreach loop we have one more for loop to get the entire column cells of related header in grid view.

foreach(DataGridViewRow row in this.Gvorders.Rows)
{
if (row.HeaderCell.Value == txtorder.Text)
{
for (int i = 0; i < Gvorders.Columns.Count; i++)
{
this.Gvorders.CurrentCell = row.Cells[i];
row.Cells[i].Style.BackColor = Color.Green;
}
}
}
VB:
For Each row As DataGridViewRow In Me.Gvorders.Rows
 If row.HeaderCell.Value = txtorder.Text Then
  For i As Integer = 0 To Gvorders.Columns.Count - 1
   Me.Gvorders.CurrentCell = row.Cells(i)
   row.Cells(i).Style.BackColor = Color.Green
  Next
 End If
Next
Post a Comment

Bel