Share via

DataGridViewRow duplication

rahul kumar 605 Reputation points
2023-06-30T10:02:47.78+00:00

I am trying to edit the cell value in my customdatagridview but when i tries to edit the value (not insert new) in the column2 again , it again adds the row1 and row2 .instead row1 and row2 should not gets created for that cell in column2 but the values in row1 and row2 should be updated

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();
        }

       protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
        {
            base.OnCellValueChanged(e);
            if (SelectedCells.Count > 0)
            {
                DataGridViewCell selectedCell = SelectedCells[0];

                if (selectedCell.ColumnIndex == 1 && (selectedCell.RowIndex % 3 == 0))
                {
                    string name = Rows[selectedCell.RowIndex].Cells[0].Value.ToString();
                    cusList.TryGetValue(name, out string value);

                    string price = selectedCell.Value.ToString();
                    DataGridViewRow row1 = new DataGridViewRow();
                    row1.CreateCells(this, $"Cur Bal:{value} Dr", "");

                    DataGridViewRow row2 = new DataGridViewRow();
                    row2.CreateCells(this, "On Account", $"{price} cr");

                    Rows.Add(row1);
                    Rows.Add(row2);
                }
            }

 
Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | 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.

0 comments No comments

Answer accepted by question author

Anonymous
2023-07-03T07:38:23.0666667+00:00

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

7_3

I used the previous code.

You just need to check whether the next line of the target line is a new line. If yes, create two new rows, if not, update the third row.

using System.Collections.Generic;
using System.Windows.Forms;

namespace _7_3_x
{
    public partial class Form1 : Form
    {
        Dictionary<string, string> cusList = new Dictionary<string, string>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            cusList.Add("deepak(market)", "21.00");
            cusList.Add("xxx(market)", "10.00");
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.SelectedCells.Count > 0)
            {

                DataGridViewCell selectedCell = dataGridView1.SelectedCells[0];

                if (selectedCell.ColumnIndex == 1 && (selectedCell.RowIndex % 3 == 0))
                {
                    string name = dataGridView1.Rows[selectedCell.RowIndex].Cells[0].Value.ToString();
                    cusList.TryGetValue(name, out string value);

                    string price = selectedCell.Value.ToString();

                    if (dataGridView1.Rows[selectedCell.RowIndex + 1].IsNewRow)
                    {
                        DataGridViewRow row1 = new DataGridViewRow();
                        row1.CreateCells(dataGridView1, $"Cur Bal:{value} Dr", "");

                        DataGridViewRow row2 = new DataGridViewRow();
                        row2.CreateCells(dataGridView1, "On Account", $"{price} cr");

                        dataGridView1.Rows.Add(row1);
                        dataGridView1.Rows.Add(row2);
                    }
                    else
                    {
                        dataGridView1.Rows[selectedCell.RowIndex + 2].Cells[1].Value = $"{price} cr";
                    }
                }
            }
        }
    }
}

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.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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