Datagridview Formatting issue on loading

rahul kumar 605 Reputation points
2023-07-04T08:27:47.9366667+00:00

I have used the following code on my customdatagridview , but found out that it is not working properly

  public partial class Paymentdatagridview : DataGridView
    {
              
        public Paymentdatagridview()
        {
            
            // i have set enableheadervisualstyles to false 
            // make bold the headers
            // Add the columns to your custom DataGridView
            DataGridViewColumn column1 = new DataGridViewTextBoxColumn();
            column1.Name = "Column1";
            column1.HeaderText = "Particulars";
            column1.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            column1.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Columns.Add(column1);

            DataGridViewColumn column2 = new DataGridViewTextBoxColumn();
            column2.Name = "Column2";
            column2.HeaderText = "Amount";
            column2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            column2.SortMode = DataGridViewColumnSortMode.Programmatic;
            this.Columns.Add(column2);

            this.AllowUserToAddRows = true;
            this.RowHeadersVisible = false;
            this.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9.75F, FontStyle.Bold);
            this.CellBorderStyle = DataGridViewCellBorderStyle.None;        

            InitializeComponent();
        }

User's image

Above is the image when it is loaded

below is the image when i starts working in it

User's image

There are multiple issues in the above

  1. I have used the following code
 

so that the backgroundcolour of datagridview should be same as of the parent but when the datagridview loads , i found out that the backgroundcolor of datagridview of first row is different.

  1. after i starts working in it . the initial white color continues to take over the new rows and so on .
  2. after i starts working in it . the initial white color continues to take over the new rows and so on .
  3. I am trying to set the font to be bold but it is also not working for 1st row .

User's image

 protected override void OnParentChanged(EventArgs e)     
   {         
   base.OnParentChanged(e);      
               if (this.Parent != null)     
       {           
     this.BackgroundColor = this.Parent.BackColor;     
       }     
   }
Developer technologies Windows Forms
Developer technologies .NET Other
Developer technologies C#
{count} votes

Accepted answer
  1. Anonymous
    2023-07-05T07:12:29.8733333+00:00

    Hi @rahul kumar , Welcome to Microsoft Q&A.

    Regarding the background color of the cell you need to set it separately:

    protected override void OnParentChanged(EventArgs e)
    {
        base.OnParentChanged(e);
        if (this.Parent != null)
        {
            this.BackgroundColor = this.Parent.BackColor;
            this.DefaultCellStyle.BackColor = this.Parent.BackColor;
        }
    }
    

    Regarding the bolding of key information lines, you may be using some kind of wrong method. According to the previous code, you can modify the font of the current row when adding a new row.

    protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
    {
        base.OnCellValueChanged(e);
    
        if (this.SelectedCells.Count > 0)
        {
    
            DataGridViewCell selectedCell = this.SelectedCells[0];
    
            if (selectedCell.ColumnIndex == 1 && (selectedCell.RowIndex % 3 == 0))
            {
                string name = this.Rows[selectedCell.RowIndex].Cells[0].Value.ToString();
                string price = selectedCell.Value.ToString();
                // Set current row's font style
                this.Rows[selectedCell.RowIndex].DefaultCellStyle.Font = new Font(this.DefaultCellStyle.Font, FontStyle.Bold);
    
                if (this.Rows[selectedCell.RowIndex + 1].IsNewRow)
                {
                    DataGridViewRow row1 = new DataGridViewRow();
                    row1.CreateCells(this, $"Cur Bal:{name} Dr", "");
    
                    DataGridViewRow row2 = new DataGridViewRow();
                    row2.CreateCells(this, "On Account", $"{price} cr");
    
                    this.Rows.Add(row1);
                    this.Rows.Add(row2);
                }
                else
                {
                    this.Rows[selectedCell.RowIndex + 2].Cells[1].Value = $"{price} cr";
                }
            }
        }
    }
    

    enter image description here

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.