Could someone help me with my problem here? I am using C# and working on a project in visual studio 2008 with SQL server express as the database. In visual studio 2008, I have a project and two forms. The first form that is named 'Informations' has a single datagridview included. I populated the datagridview by selecting all fields in my table in SQL and then binded it to a dataset.

SqlCommand cmd = new SqlCommand("select * from myTable", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

myDataGridview.DataSource = ds.Tables[0];

The table myTable is consist of three columns namely 'ID', 'LastName', 'FirstName'.. Using the code above I successfully binded the dataset to the datagridview named 'myDataGridview'.

The second form that is named 'PassedInformations" has three textbox. I have used the constructor method to pass the selected values from form Information's datagridview to form PassedInformation's three textbox.

string txtbox1 = myDataGridview.CurrentRow.Cells[0].Value.ToString();
string txtbox2 = myDataGridview.CurrentRow.Cells[1].Value.ToString();
string txtbox3 = myDataGridview.CurrentRow.Cells[2].Value.ToString();

//show PassedInformations and fill the textboxes with datagridview's selected value
PassedInformation information = new PassedInformation(txtbox1, txtbox2, txtbox3)
information.ShowDialog();

Then in form PassedInformation I place the passed string to the constructor method.

All works well but I was suddenly required by my professor to only show the 'Last Name' and 'First Name' column in myDataGridview but still capable of passing the values of 'ID', 'Last Name' and 'First Name'.

I think I need to passed columns of dataset instead the datagridview because the values that would be pass dependeds to the selected row/cell of myDataGridview, but the fact that a column is not present and still required to be pass is quite improbable to me. How could I determine the selected value of the datagridview then still pass the 'ID' which is not present in myDataGridview.

Recommended Answers

All 6 Replies

In your first form put something like this:

this.dataGridView1.Columns["ID"].Visible = false;

It will hide the column but the data will still be there for you to use.

The 'ID' column is the first column of the datagridview. Would the selection considers the 'ID' column that is not visible to be the first cell?

myDataGridview.CurrentRow.Cells[0].Value.ToString();

If made the 'id' column not visible, then the first cell of the current selected row would be the 'Last Name' column?

No, the column is still there and nothing else changes except the visibility of the column.

Better, after getting the values from the database, U copy the Dataset into another dataset. Now delete the Column values of ID from this Dataset and give to the DataSource. When Passing to the Textboxes based on the Selected Row Text in the Gridview, search the relavent values in the Dataset and Pass the results to the Text boxes.
Hope it will help You,
Regards
Kalyan.

Better, after getting the values from the database, U copy the Dataset into another dataset. Now delete the Column values of ID from this Dataset and give to the DataSource. When Passing to the Textboxes based on the Selected Row Text in the Gridview, search the relavent values in the Dataset and Pass the results to the Text boxes.
Hope it will help You,
Regards
Kalyan.

This is better? Why? It seems a lot of work.

Just, i have said my Idea. It doesn't mean that u must accept it.

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.