locked
Datagridview cell click and text become bold c# RRS feed

  • Question

  • i have datagridview with three textbox columns. my datagridview few property called enable editing/adding/deleting all are false. i add rows to my Datagridview dynamically. here is my code

    public partial class dgChatConversion : Form
        {
            public dgChatConversion()
            {
                InitializeComponent();
    
                dgView.ReadOnly = false;
                DataGridViewCellStyle dgvCellStyle = new DataGridViewCellStyle();
                dgvCellStyle.WrapMode = DataGridViewTriState.True;
    
                dgView.Columns[0].DefaultCellStyle = dgvCellStyle;
                dgView.Columns[1].DefaultCellStyle = dgvCellStyle;
                dgView.Columns[0].Width = 140;
                dgView.Columns[1].Width = 480;
                dgView.Columns[2].Width = 69;
    
                dgView.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
                dgView.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
                dgView.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight;
    
                dgView.Columns[0].DefaultCellStyle.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular);
                dgView.Columns[1].DefaultCellStyle.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular);
                dgView.Columns[2].DefaultCellStyle.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular);
    
    
            }
    
            private void btnSend_Click(object sender, EventArgs e)
            {
                dgView.Rows.Add(txtUser.Text, txtMsg.Text, DateTime.Now.ToString("HH:mm tt"));
                dgView.ClearSelection();
                dgView.CurrentCell = null;
            }
    
            private void dgView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (e.Control is TextBox)
                {
                    (e.Control as TextBox).Focus();
                    (e.Control as TextBox).ReadOnly = true;
                    (e.Control as TextBox).SelectionStart = 0;
                    (e.Control as TextBox).SelectionLength = 0;
                }
            }
    
    
            private void dgView_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                int Rowindex = e.RowIndex;
                int CellIndex = e.ColumnIndex;
                dgView.Rows[Rowindex].Cells[CellIndex].Style.Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular);
            }
    
    
        }

    my problem is when i click on Datagridview cell then that cell's text become bold or dark black color but i have not wrote any such code for that.

    i want that when i will click on any cell then text black color should be more dark rather it looks as usual. what i need to change. guide me. thanks

    here i am giving the images. one image is before click on cell and another image is after click on cell. after clicking on cell the cell text fore color become dark...Why?

    • Moved by CoolDadTx Friday, March 29, 2013 2:14 PM Winforms related
    • Edited by Mou_kolkata Friday, March 29, 2013 2:59 PM
    Friday, March 29, 2013 11:45 AM

All replies

  • Since ur CellClick's Style is the same as what you've defined in ur initialization function, why do u re-define it again? 

    Remove it.


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    Friday, March 29, 2013 12:07 PM
  •     viewDataGridView.CellContentClick += (s, e) =>
                    {
                        if (e.RowIndex != -1 && e.ColumnIndex != -1)
                        {
                            viewDataGridView[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Red;
                        }
                    };

    or

    public void viewDataGridView_CellContentClick ( object sender, EventArgs e )

    {

      if (e.RowIndex != -1 && e.ColumnIndex != -1)
                        {
                            viewDataGridView[e.ColumnIndex, e.RowIndex].Style.ForeColor = Color.Red;
                        }

    }

    Friday, March 29, 2013 12:09 PM
  • my problem is when i click on any cell then cell's text fore color become dark black seem like highlighted. which i do not want. the above trick does not work.
    Friday, March 29, 2013 1:33 PM
  • my problem is when i click on any cell then cell's text fore color become dark black seem like highlighted. which i do not want. the above trick does not work.
    Friday, March 29, 2013 1:33 PM
  • Try this , its Helpful

    public void viewDataGridView_CellContentClick ( object sender, EventArgs e )

    {

      if (e.RowIndex != -1 && e.ColumnIndex != -1)
                        {

     viewDataGridView[e.ColumnIndex, e.RowIndex].Style = new DataGridViewCellStyle { Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold) };

    }

    }

    Friday, March 29, 2013 2:11 PM
  • here i paste the images one before click on cell text and another one after click on cell. please have a look and tell me why cell text fore color getting dark when i click on cell. thanks
    Friday, March 29, 2013 3:00 PM
  • here i paste the images one before click on cell text and another one after click on cell. please have a look and tell me why cell text fore color getting dark when i click on cell. thanks
    Friday, March 29, 2013 3:00 PM
  • it did not work out. just see my images.
    Friday, March 29, 2013 3:16 PM
  • Submit ur proj to SkyDrive so as to let us analyze....

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    Saturday, March 30, 2013 2:02 AM
  • thanks for ur help. there was problem later i found in code and now it is fixed. thanks a lot.
    Saturday, March 30, 2013 3:36 PM
  • thanks for ur help. there was problem later i found in code and now it is fixed. thanks a lot.
    Can u show us your solution and mark it as an answer?

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    Sunday, March 31, 2013 4:44 AM