Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagridview dynamically add imagecolumn and populate it

Status
Not open for further replies.

G00GLER

Instructor
May 17, 2005
57
0
0
US
if i have a datagridview that is populated with an xml file.

I have images in a resource file that i'd like to show in the datagridview based on values in one of the column say light

Code:
foreach (DataRow dr in myDataSet.Tables["tblXMLExport"].Rows)
{
  switch (dr["Light"].ToString())
  {
    case "YELLOW":
      DONTKNOWWHATTOPUTHERE = NewModel.Properties.Resources.yellow;
      break;
  }
}

I was thinking I could use int i and i++ through the foreach loop and that way reference DataGridView my column and row, but I don't know how to reference the column in datagridview1 also not sure if i am supposed to user setvalues()

any pointer very much appreciated.
 
Use the CellFormatting event and change the argument value to the required image:
Code:
static void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridView dgv = sender as DataGridView;

    if (dgv.Columns[e.ColumnIndex].DataPropertyName == "TheBoundColumnName")
	{
	    DataRow dr = ((DataRowView)dgv.Rows[e.RowIndex].DataBoundItem).Row;

	    switch (dr["Light"].ToString())
            {
                case "YELLOW":
                    {
                        e.Value = NewModel.Properties.Resources.yellow;

                        break;
                    }
            }
        }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top