Textbox's color within datagridview

OmkarHcl 206 Reputation points
2023-06-06T14:02:10.7366667+00:00

Hi , I am trying to set the backcolor of textbox within the datagridview to red but i found out that the color is not filling in the textbox properly . it is leaving some space from all the four sides . I have tried setting borderstyle to none and also tried padding , but none of them worked. Can you please suggest .

        private void customControl11_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.KeyPress -= new KeyPressEventHandler(textbox_keypress);
            e.Control.TextChanged -= new EventHandler(TextBox_TextChanged);
            e.Control.KeyDown -= new KeyEventHandler(TextBox_KeyDown);
            e.Control.Enter -= new EventHandler(TextBox_Enter);
            if (customControl11.CurrentCell.ColumnIndex == 1 || customControl11.CurrentCell.ColumnIndex == 2) // Assuming the first column has an index of 0
            {
                TextBox textBox = e.Control as TextBox;
                if (textBox != null)
                {
                    textBox.KeyPress += new KeyPressEventHandler(textbox_keypress);
                }
            }

            else if (customControl11.CurrentCell.ColumnIndex == 0) // Assuming the first column has an index of 0
            {
                TextBox textBox = e.Control as TextBox;
                if (textBox != null)
                {
                    textBox.TextChanged += TextBox_TextChanged; // Attach TextChanged event handler
                    textBox.KeyDown += TextBox_KeyDown; // Attach KeyDown event handler
                    textBox.Enter += TextBox_Enter;
                }
            }
        }
        private void TextBox_Enter(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            textBox.BackColor = Color.Red;
            textBox.BorderStyle = BorderStyle.None;
            // Set padding and margin to zero
            textBox.Margin = new Padding(0);
            textBox.Padding = new Padding(0);
            if (customControl11.CurrentCell.ColumnIndex > 0)
                return;

            dataGridView1.Visible = true;
            dataGridView1.Columns["id"].Visible = false;
            dataGridView1.Columns["current_balance"].Visible = false;
            dataGridView1.Columns["opening_balance"].Visible = false;
            dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[1];
        }

here is the output enter image description here

you can see that the red color is not filling properly .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,907 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,096 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 47,581 Reputation points Microsoft Vendor
    2023-06-08T09:14:40.6633333+00:00

    Hi @OmkarHcl , Welcome to Microsoft Q&A.

    Updated:

    Just add the dataGridView1_CellFormatting event:

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)
        {
            e.CellStyle.BackColor = Color.Red;
        }
        else
        {
            e.CellStyle.BackColor = dataGridView1.DefaultCellStyle.BackColor;
        }
    }
    

    6_9_2


    Note the RowsDefaultCellStyle.BackColor property.

    You just need to use the code:

     private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red;
    }
    

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. Osjaetor 480 Reputation points
    2023-06-06T18:01:34.44+00:00

    Hi ,

    I suggest these changes to your code to get everything filled in:

    private void customControl11_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        //Your code...
    
        else if (customControl11.CurrentCell.ColumnIndex == 0)
        {
            TextBox textBox = e.Control as TextBox;
            if (textBox != null)
            {
                textBox.TextChanged += TextBox_TextChanged;
                textBox.KeyDown += TextBox_KeyDown;
                textBox.Enter += TextBox_Enter;
    
                // Set the cell style to remove padding and margin
                dataGridView1.DefaultCellStyle.Padding = new Padding(0);
                dataGridView1.DefaultCellStyle.Margin = new Padding(0);
            }
        }
    }
    
    private void TextBox_Enter(object sender, EventArgs e)
    {
        TextBox textBox = sender as TextBox;
        textBox.BackColor = Color.Red;
        textBox.BorderStyle = BorderStyle.None;
    }
    

    Regards,


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.