How to Color the User entered cell in Datagridview?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KrishnaSarala
    New Member
    • Dec 2009
    • 4

    How to Color the User entered cell in Datagridview?

    Hi All IT developer and C# expert please help me urgent.
    I'm working on a project of Bakery Management. I'hd shown the room booked in hotel in Datagridveiw Control. My client require the cell color in different colors when he enters in particular cell. I had written code in dgv_Formatting Event for coloring but all cells of Datagriveiw are going to colored. I write code like follow.
    private void dgv_CellFormatt ing(object sender, DataGridViewCel lFormattingEven tArgs e)
    {

    e.CellStyle.Bac kColor = Color.MediumPur ple;

    }
    Please help me urgent.
  • KrishnaSarala
    New Member
    • Dec 2009
    • 4

    #2
    How to Color the User entered cell in Datagridview?

    hi all it & c# expert help me
    i want to color the cell of datagridview when user enters in particuler cell.
    ihad write code like this.
    private void dgv_CellFormatt ing(object sender, DataGridViewCel lFormattingEven tArgs e)
    {

    e.CellStyle.Bac kColor = Color.MediumPur ple;

    }
    but all the cells are going to coloured. please help me.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      Maybe instead of the cell formatting event, try the cell value changed event?

      I did up a quick example and it worked. Here's the appropriate snippets...

      Code:
              public Form1()
              {
                  ...
                  dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
              }
      
              ...
      
              void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
              {
                  DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                  row.Cells[e.ColumnIndex].Style.BackColor = Color.MediumPurple;
              }
      This worked for me... let me know how it works for you :) I'd imagine the access is relatively safe since the row and column index comes from the event, but you may want to put some bounds checking in there anyway.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Please don't double-post your questions. It divides attempts to help you in an organized and cohesive manner. Your threads have been merged

        Comment

        Working...