Unbound dgv - save two of the columns to a join table

  • Thread starter Thread starter dbuchanan
  • Start date Start date
D

dbuchanan

Hello,

I have a dgv populated as the result of a dragDrop. It will contain 5
columns. The datagrid view is unbound.

The dragDrop operation allows the user to select value. Those selected
values are associations that I then want to make by saving off two of the
columns to the join table?

How do I do save two of the columns to the join table. (I will then empty
the target table and refresh another dgv that will show the results.)

Thank you,
Douglas
 
Hi Douglas,
Those selected values are associations that I then want to make by saving
off two of the columns to the join table?

I couldn't understand what you mean in the above sentence. Would you please
explain what you want to do with an example? I think that would help us to
understand your scenario and question.

I look forward to your reply!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Linda,

dgv populated as the result of a dragDrop;
Columns:
fk pk Matl yp Cate Dept
-- -- ----- --- ---- -----
6 3 2x4x8 Fir Dime Struc
6 4 2x6x8 Fir Dime Struc
6 8 4x8x.2 Ply Plyw Struc
6 9 4x4x8 Prs Dime Struc

I want to save the values above and find the following new row values in the
two column Join table
fk pk
-- --
6 3
6 4
6 8
6 9

Remember that the dgv (populated as the result of a dragDrop) is unbound.

Thank you,
Douglas
 
Hi Douglas,

Thank you for your reply and detailed information!

If the "join table" you mentioned is a DataTable, you can save the values
of under the first two columns in the DataGridView to the DataTable as
follows:

DataTable jointable = new DataTable();
private void button1_Click(object sender, EventArgs e)
{
DataRow row;
int rowcount = 0;
if (this.dataGridView2.AllowUserToAddRows)
{
rowcount = this.dataGridView2.RowCount - 1;
}
else
{
rowcount = this.dataGridView2.RowCount;
}

for (int i = 0; i < rowcount; i++)
{
row = jointable.NewRow();
row[0] = this.dataGridView2.Rows.Cells[0].Value;
row[1] = this.dataGridView2.Rows.Cells[1].Value;
jointable.Rows.Add(row);
}
}

If the "join table" is a database table, you can make use of the
SqlCommand/OleDbCommand to save the values back to the database. Of course,
you need to create and open a SqlConnection/OleDbConnection and configure
the command text of the SqlCommand/OleDbCommand.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Linda,

There is something I need to do before I add all the records to the join
table.

Since the user could conceivably select records he selected on a previous
occassion and since the join table only allows unique column combinations
for records how can I filter out all the existing records where the two
columns exist together alread in the join table?

I want to filter out the records that already exist in the join table. I
would not have a problem with this if it were mearly a query construction
and the data existed in the database in another table. What I do not know
how to do is process this under these circumstances.

One thought I had was to put the records into a temporarty table and then
filter that temporary table with a query. Again I would know how to do this
if it were a temp table in SQL, but again I do not know how to implement
this under the circumstqances within the form.

Would you please get me started?

Thank you,
Douglas
 
Hi Douglas,
I want to filter out the records that already exist in the join table.

If the join table you mentioned is a DataTable, I suggest that you check
whether the record to be added into the DataTable exists already in the
DataTable before adding this record to the DataTable.

To do this, you can define the unique columns as the primary key of the
DataTable and then use the DataTable.Rows.Contains method to check if the
specified keys exists in the DataTable already.

The following is a sample:

Dim table As New DataTable

'add two DataColumns to the DataTable
Dim column As New DataColumn("column1")
table.Columns.Add(column)
column = New DataColumn("column2")
table.Columns.Add(column)

'set the DataTable's PrimaryKey
table.PrimaryKey = New DataColumn() {table.Columns(0),
table.Columns(1)}

'add a DataRow into the DataTable
Dim row As DataRow = table.NewRow()
row(0) = "1"
row(1) = "1"
table.Rows.Add(row)

'the variable result is True after this line of code is executed
Dim result As Boolean = table.Rows.Contains(New String() {"1", "1"})

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Linda,

This clears things up for me.Especially the line:
Dim result As Boolean = table.Rows.Contains(New String() {"1", "1"})

Thank you for your help.

Douglas
 
Back
Top