none
Disable default row selection / cell selection in datagridview

    Question

  • Hello All,

    I am manually adding a rows to a datagrid view.

    everytime the first row gets selected and turns dark blue in the background color.

    my problem is sometimes I need to show red color on error. So I need to disable the default cell selection .

    how do i make a setting so tat no row gets selected in the grid by default.

    thanks

    sandeep

    Friday, December 24, 2010 2:32 AM

Answers

  • My question was , in which event you are changing this color?, If you want to do this based on cell values you should do it in CellFormatting Event. But there will be performance hits using this method. Better you go through this programming guidelines while using DefaultCellStyle property.

    http://msdn.microsoft.com/en-us/library/ha5xt0d9.aspx

    Check this sample.

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
      // If the column is the Artist column, check the
      // value.
      if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist")
      {
        if (e.Value != null)
        {
          // Check for the string "pink" in the cell.
          string stringValue = (string)e.Value;
          stringValue = stringValue.ToLower();
          if ((stringValue.IndexOf("pink") > -1))
          {
            e.CellStyle.BackColor = Color.Pink;
          }
    
        }
      }
      else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")
      {
        ShortFormDateFormat(e);
      }
    }
    
    
    

     

     

    • Marked as answer by Helen Zhou Tuesday, January 04, 2011 9:36 AM
    Friday, December 24, 2010 4:13 AM
  • if you have 1000' of rows, better not to load all together, use 'paging'. I mean load only some fixed amount at a time. There is some samples like this http://www.codeproject.com/KB/miscctrl/Pagable_DatagridView.aspx

    but altogether hanging (which happens if I override onpaint method) should not happen....

    what is the purpose of overriding paint event?

    • Marked as answer by Helen Zhou Tuesday, January 04, 2011 9:35 AM
    Friday, December 24, 2010 6:09 AM

All replies

  • I cannot change the color in the default cell style properties. pls help me on hw to do ..
    Friday, December 24, 2010 2:40 AM
  • You need to use DataGridView.RowsAdded Event. When the user adds a new row using the row for new records, the DataGridViewRowsAddedEventArgs.RowIndex value in the handler for this event is equal to the index of the new location of the row for new records, which is one greater than the row just added. When you add rows programmatically, however, the RowIndex value is the index of the first row added. In this event you will able to control current selection based on the RowIndex value.

    Refrence http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowsadded.aspx

    I cannot change the color in the default cell style properties. pls help me on hw to do ..

    Can you post the code?

     

    Friday, December 24, 2010 3:31 AM
  • Hi Mike,

    what I meant is when I add few rows in teh grid. I change the background color to red indicating error rows.

    so by changing default cell style background color to window..my red color does not appear instead appears as transparent.

    thanks

     

    Friday, December 24, 2010 3:57 AM
  • My question was , in which event you are changing this color?, If you want to do this based on cell values you should do it in CellFormatting Event. But there will be performance hits using this method. Better you go through this programming guidelines while using DefaultCellStyle property.

    http://msdn.microsoft.com/en-us/library/ha5xt0d9.aspx

    Check this sample.

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
      // If the column is the Artist column, check the
      // value.
      if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist")
      {
        if (e.Value != null)
        {
          // Check for the string "pink" in the cell.
          string stringValue = (string)e.Value;
          stringValue = stringValue.ToLower();
          if ((stringValue.IndexOf("pink") > -1))
          {
            e.CellStyle.BackColor = Color.Pink;
          }
    
        }
      }
      else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")
      {
        ShortFormDateFormat(e);
      }
    }
    
    
    

     

     

    • Marked as answer by Helen Zhou Tuesday, January 04, 2011 9:36 AM
    Friday, December 24, 2010 4:13 AM
  • Hello Mike..

    Yes I found one post on the internet and started doing coloring in the cell formating event.

    but yes there is a perfrmance hit. i hope it does not hang when trying to color too many rows..

    like thousands of rows.. need help on alternatives..

    do u suggest third party controls or use default windows controls.

    thanks

    Sandeep

    Friday, December 24, 2010 4:16 AM
  • if you have 1000's of row better not to use this method. You can compare the performance of third party control.
    Friday, December 24, 2010 5:45 AM
  • Hi Mike.. I can see the performance hits..

    but what I do not want is the screen to hang while loading. i am ok with 2-3 seconds of delay..

    but altogether hanging (which happens if I override onpaint method) should not happen....

    pls help

    thanks

    Friday, December 24, 2010 5:58 AM
  • if you have 1000' of rows, better not to load all together, use 'paging'. I mean load only some fixed amount at a time. There is some samples like this http://www.codeproject.com/KB/miscctrl/Pagable_DatagridView.aspx

    but altogether hanging (which happens if I override onpaint method) should not happen....

    what is the purpose of overriding paint event?

    • Marked as answer by Helen Zhou Tuesday, January 04, 2011 9:35 AM
    Friday, December 24, 2010 6:09 AM
  • there is no specific purpose. just tried out different ways... to see if the grid hangs while loading 1000 colored rows..
    Friday, December 24, 2010 8:06 AM
  • Mike pls help me with my original doubt..

    even though i want to show red color row on error.

    the blue color overlaps the red color error row.. coz of default selection.

    hwo do i proceed. thanks

    Friday, December 24, 2010 8:20 AM
  • Based on the current value of 'Color' you should set background color which will make more sense and grid will look more stable. Make sure you set ForeColor & BackFolor  according to default section/user selection.

    Friday, December 24, 2010 8:51 AM
  • To stop the DataGridView selecting the first row by default, use the DataBindingComplete event and clear the selection:

    private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
       dataGridView1.ClearSelection();
    }
    


    Kev Ritchie
    Follow me on Twitter
    Check out my blog
    Friday, December 24, 2010 9:40 AM
  • Hi Kev, i am manually adding each rows  i am not doing data binding..

    i tried clearselection. it dint work.. can u pls help with any other technique.

    thanks

    Friday, December 24, 2010 9:48 AM
  • This should do the trick:

    private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
       if (dataGridView1.Rows[0].Selected)
       {
         dataGridView1.Rows[0].Selected = false;
       }
    }
    


    Kev Ritchie
    Follow me on Twitter
    Check out my blog
    Friday, December 24, 2010 10:51 AM
  • This should do the trick:

     

    private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    {
      if (dataGridView1.Rows[0].Selected)
      {
       dataGridView1.Rows[0].Selected = false;
      }
    }
    

     


    Kev Ritchie
    Follow me on Twitter
    Check out my blog

    nope it wont.

    1st row will still be selected. Belive me, I had these kind of issue for a long time, but then I found by my self a trick solution - to set the background color to the same as the other cells have when not selected:

     

       if (dataGridView1.Rows.Count > 0)
       {
          dataGridView1.Rows[0].Selected = false;
          dataGridView1.ClearSelection();
          dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.White;
       }
    
    try it, it cant hurt, but it works - at least for me.

    Friday, December 24, 2010 11:14 AM
  • Well spotted Mitja :)

    Just had another play and came up with this, use the RowStateChanged event:

     private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
    {
      if (dataGridView1.CurrentCell != null)
      {
         dataGridView1.CurrentCell.Selected = false;
      }
    }
    


    Kev Ritchie
    Follow me on Twitter
    Check out my blog
    Friday, December 24, 2010 11:46 AM
  • thank you.. i shall try that out.. will keep you updated.. thanks
    Friday, December 24, 2010 11:58 AM
  • Hi Sand,

    For disable selection of rows in a datagridview, please refer to http://efreedom.com/Question/1-71423/Disable-Selection-Rows-Datagridview

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 04, 2011 7:31 AM
  • Hi Kev, i am manually adding each rows  i am not doing data binding..

    i tried clearselection. it dint work.. can u pls help with any other technique.

    thanks


    Didn't work? It works on my side perfectly as expected. Where did you add the dataGridView1.ClearSelection();? dataGridView1_RowsAdded would be ok. Coding like this:

    private void Form1_Load(object sender, EventArgs e)

            {

                dataGridView1.Columns.Add("sss", "1");

                dataGridView1.Columns.Add("ss2s", "2");

                dataGridView1.Rows.Add("s", "s");

                dataGridView1.Rows.Add("s1", "s11");

                dataGridView1.Rows.Add("s2", "s22");

                dataGridView1.Rows.Add("s3", "s33");

                dataGridView1.Rows.Add("s4", "s44");

                dataGridView1.AllowUserToAddRows = true;   

            }

     

            private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)

            {

                dataGridView1.ClearSelection();

            }

    Regards,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 04, 2011 9:45 AM
  • Hello Helen, I am looking into this issue.. will try it out and keep you updated on the same.

    thanks

    Sandeep

    Thursday, January 06, 2011 12:49 PM