Merging two Datagridview as one datagridview

Shaifali jain 420 Reputation points
2023-09-19T11:40:59.45+00:00

Hello ,

On my windows form , i have a created a usercontrol with tablelayout panel with two columns (no rows ) each equal in size . In each column i have a datagridview . so there is total of 2 datagridview's . Now 1st datagridview shows some imformations like

User's image

and

datagridview2

User's image

now when combined inside a usercontrol as i said above it looks like the below


User's image

what i am trying is get two things in both datagridview

  1. when the focus reaching salary row and the user presses enter , some additional rows with columns , beneath salary should gets created but these row cannot be selected . in my case there are 2 rows A and B
  2. to change the default behavior of navigation keys in both the datagridview . so that both works like a single control . lets say if the user is on cleaning row and if he choose to move to sale row of 2nd datagridview , he just have to press left arrow key on his keyboard .

I would be highly thankful for any expert who suggests how to get the both things in my datagridview as i have searched everything till i can .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 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.
10,839 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 44,411 Reputation points Microsoft Vendor
    2023-09-20T03:38:33.8666667+00:00

    Hi @Shaifali jain , Welcome to Microsoft Q&A,

    Regarding your first question, I suggest you add the three columns manually. The middle column is used to store the data you need alone.

    // Assume your data source is a List<YourClass>
    List<YourClass> yourDataSource = GetYourDataSource(); // Method to get the data source
    
    //Assign the data source to the DataGridView
    dataGridView1.DataSource = yourDataSource;
    
    // Explicitly specify the association of each column with the data source
    dataGridView1.Columns["Column1"].DataPropertyName = "PropertyName1"; // Associate "Column1" with the "PropertyName1" column of the data source
    dataGridView1.Columns["Column2"].DataPropertyName = "PropertyName2"; // Associate "Column2" with the "PropertyName2" column of the data source
    dataGridView1.Columns["Column3"].DataPropertyName = "PropertyName3"; // Associate "Column3" with the "PropertyName3" column of the data source
    

    Depending on how you want to use the data, you can also just use three columns of data. But it is more difficult to add data with two columns in one column alone. (You may need to use custom controls, and subsequent operations will be difficult)

    Regarding making the created row unselectable: When creating a new row, add a tag to it. And paired with DataGridViewSelectionMode.FullRowSelect so that only the entire row can be selected. Then use the dataGridView1_RowStateChanged event:

    private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
    {
         if (e.Row.Tag == "xxx")
         {
             e.Row.Selected = false;
         }
    }
    

    You can also use the ReadOnly attribute alone to make the line unmodifiable.

    Regarding the second question: You may need to customize the keydown event: determine the focus and then move the focus to where you want it.

    Use 'e.Handled = true' to cancel the original operation.

    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.