Hello buddies, I have two datagrid views on my form.
The first datagridview is bound to a table named contacts. And the second one is bound to a different tables say Group_members. Now My problem is that I want to add the selected row in DataGridView1 to datagridview2. And later save the changes. Please please help me..Only two days left to accomplish the project.
Thanks.

Recommended Answers

All 7 Replies

I'm confused : / If the datagridviews are bound to different tables, what are you trying to acheive by transposing rows?
Can you show the table structures? If your database is normalised correctly you should be storing data specific to the contact in the Contacts table. Why would you want to replicate that data in the Group Members table?

I'm assuming (and without seeing your table, this is only a guess) that the Group Members table is used to store which groups a contact is a member of. If thats the case, the Group Members table should be a link table holding the primary key of the contact table and the primary key of the groups table:

CONTACT TABLE
ContactID (pk) int
ContactName string

GROUP TABLE
GroupID (pk) int
GroupName string

GROUP_MEMBERS TABLE
MemberID (composite pk)
GroupID (composite pk)

Thanks for your response, Your guess is absolutely correct, and I apologize for confusing you. The situation is that both the datagridviews will be bound to the table contacts. The first datagridview will hold all the records. But the second one will hold the records for selected group.
I have attached a screen shot so that u may get an idea about what I am trying to do. I have used nvarchar data type for storing contact_names and contact_numbers. Whereas IDs are of int data type.

Ok, that clears up my confusion about moving a contact into the datagridview. But i think you are approaching this the wrong way. The datagridview should just show the data as it is in the database. You make changes to the underlying data, then repopulate the datagridview. So you dont move a row from one datagridview to the other. You add a new record to the Group_Members table and refresh the second datagridview.

OK I understood what you said, But can you please tell me the logic about how to do it.
I am a bit confused. I have Group_ID and Member_ID in group_members but How to select the contacts by Selecting the Group_ID from Group_Members. As My dataGridview2 is bound to table contacts. How can I view only certain contacts

you need to alter the select statement you are using to populate the second datagridview. Something like:
SELECT * FROM Contacts JOIN Group_Members ON Contacts.Member_ID = Group_Members.Member_ID
WHERE Group_Members.Group_ID = @SelectedGroupID

Then set the @SelectedGroupID to the group you wish to display

First you join the Contacts table with the GroupMembers table, then you use the WHERE clause to limit the return set to those members who are part of the chosen group.

hey thanks my dear friend.. its almost done. The only thing left is the variable in my query. The query
is as follow

SqlCeDataAdapter adp = new SqlCeDataAdapter("select * from Contacts JOIN group_members ON Contacts.ID = Group_members.member_ID where Group_Members.group_ID = '" + current_group + "'", con);

where current group is a string type...
If I write an Integer value instead of variable then the code works fine. How can i change that string variable to Integer variable

Rather than concatenate the query with the variable, use a parameter. Thats what the @SelectedGroupID is in the query i showed you.
Check here, it should show you how to work with parameters. Shout if you need any pointers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.