Jump to content

getting selected rows data from a datagridview connected to a sql database

- - - - -

  • Please log in to reply
1 reply to this topic

#1
DonIdiotoGrande

DonIdiotoGrande

    Newbie

  • Members
  • Pip
  • 2 posts
Hi every one!

I'm completely new to C# and have a real newbie problem. However, how newbie the problem seems I seem unable to find the answer googling on the net so maby it's my informationfinding skills that lacks...

Any how, here's the problem. I have two datagridviews connected to different tables in a mssql database. I want the user to be able to select one or more rows in datagridview 2, right click, chose "import" from a menu, get a new window with the selected rows and the possibility to manipulate the data and then import the rows to the database connected to datagridview 1.

The focus of the problem now is how to get the selected data from the datagridview/database. The datagridviews loads fine with data from the database, I can select rows, get the menu and chose import but then I fail.

I first tried the DataGridView.SelectedRows.CopyTo to copy the rows to a DataGridViewRow[]. The problem was that when I tried to access the individual cells by their column name the program wouldn't recognize the column name since Visual Studio for some reason named it "manufacturerDataGridViewTextBoxColumn" instead of "manufacturer" which seem a bit odd to have in the code and I haven't figured out a way to change that in Visual studio...

Later a collegue told me that that wasn't the proper way to do it, one would instead retrieve the data from the dataset associated with the datagridview, which as I has understood it is an in memory copy of the whole database table?! However he didn't at the moment know how to do that an now I'm abit lost cuse I haven't been able to find satisfying info on that either and i'm not sure which way to go, maby there's a third better way?

Well... ofcourse, with more background knowledge on .net I might be able to solve this. Maby I'm trying to build a fort on none solid ground... however.. aah.... anyone has any ideas? :) Trying to learn this...

Any help would be greatly appreciated!!

Regards
Johan
Ps. Here is my first attempt with a few lines of code. It works, but I don't like the naming and I guess there are better ways to do it..

private void toolStripMenuItemImport_Click(object sender, EventArgs e)
{
DataGridViewRow[] rows = new DataGridViewRow[100];
dataGridView2.SelectedRows.CopyTo(rows,0);
label1.Text = rows[0].Cells["eANDataGridViewTextBoxColumn"].Value.ToString();
}

#2
DonIdiotoGrande

DonIdiotoGrande

    Newbie

  • Members
  • Pip
  • 2 posts
ok, think I solved it for the moment... simple when you see it... don't know if it's the prefered way but now I'm working on the data in the dataset instead of datagridview and the columnnaming is right. Also at the moment I get rid of the 100 element fixed allocated array...

The code displays the data in the "Description" column in the first selected row.

private void toolStripMenuItemImport_Click(object sender, EventArgs e)
{
int rowIndex = dataGridView2.SelectedRows[0].Index;
label1.Text =
mydatabaseDataSet1.SupplierData[rowIndex].Description;
}


/DonIdiotoGrande




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users