Last week I started attempting to use Winforms to automate some reports. I am not enjoying the transition (as simple as it may be to some of you)!
I am currently displaying data from a SQL view that I created using Oracle SQL Developer. I accecssed the data by using the DataGridView Tasks to select the table and display it (it seemed like the most straightforward method).
All of the data displays fine. However, I would now like to access the cells in the table. I want to be able to alter the colours of the cells based upon their values and to pull out some of the data if needs be.
To access the data and highlight the rows I was hoping to do something like this
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int theValue = Convert.ToInt32(e.Row.Cells[0].Text);
if (theValue>0)
e.Row.Cells[0].BackColor = System.Drawing.Color.Red
}
}
It doesn’t work because the event is not firing. I think I am supposed to bind the data to my gridview but I have loaded all of the data through the designer. Is this the problem? I have no idea how to code it myself, I tried before but it threw a wobbly.
Furthermore, when I try to change the SQL query for the database through the designer it does not work. I think I am going to lose my mind at this rate!
You need to use the CellFormatting Event on the DataGridView like so