Additional rows within datagridview

rahul kumar 605 Reputation points
2023-06-11T13:47:48.3366667+00:00

Can someone please share a code block by which we can create a datagridview which has a two additional rows (giving details about the main row ) for every row in the datagridview .the new rows each must have two labels . I have to tried to find a similar example but failed to so . please help me out . Thanks in advance .

Payment datagridview

in the above example the first row contains two columns having values Deepak market and 200.00 respectively . as the user presses enter (after typing 200) , two rows should gets created in which 1st row should have a label giving Cur Bal: 21.00 Dr and the second additional row should have two labels On Account and 200.00 Cr .

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.
{count} votes

Answer accepted by question author
  1. Anonymous
    2023-06-14T08:26:19.83+00:00

    Hi @Rahul Kumar , Welcome to Microsoft Q&A.

    I simulated the results you get using a dictionary.

    Use the valuechange event to trigger the judgment.

    Only when the current cell is the second column every 3 cells will be triggered:

    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace _6_14_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();
                        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);
                    }
                }
            }
        }
    }
    
    

    enter image description here

    This code only demonstrates how to add two required lines as needed, you also need to add reckless judgment as needed.

    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' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.