Help needed with code

A

Aaron

I have a datagrid populated by a Dataset using an SQLAdapter. This
datagrid is at the bottom of the form displaying all of the records for my
SQL Query.

Above the datagrid, I have several textboxes corresponding to the columns
in the datagrid.

What I would like to do is be able to click on a row in the (ReadOnly)
datagrid and have the Textboxes populate with the data in that record. I
would like to make changes in the textboxes and then click an update button
which will allow the SQLUpdate command to run.

Any ideas on where I should start looking?

Thanks,
Aaron
 
G

Guest

Here is a outline of the similar requirement I had. Hope it helps.

1) In the MouseUp event for the Datagrid, get the selected record, like in this method
<CODE
private void dgTrx_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e

tr

if(e.Button != MouseButtons.Left) return

int nID = this.GetSelectedRecordID(this.dgTrx, this.dsTrx, CScenariosConst.NODE_TRANSACTION)
if(nID == 0) return

// Load the Values to the for
this.SetValues(nID)


catch(Exception ex

System.Diagnostics.Debug.WriteLine(ex.GetBaseException().ToString())


</CODE

2) This method gets the selected recor
<CODE
private int GetSelectedRecordID(DataGrid aDataGrid, DataSet aDataSet, string aTableName

tr

int nCols = aDataSet.Tables[aTableName].Columns.Count - 1

// Get the value of ID column for the selected recor
DataRow dr = aDataSet.Tables[aTableName].Rows[aDataGrid.CurrentCell.RowNumber]
DataColumn dc = aDataSet.Tables[aTableName].Columns[nCols];

string strID = dr[dc].ToString()
if(strID != null && strID != ""
return Convert.ToInt32(strID)

return 0

catch(Exception ex

System.Diagnostics.Debug.WriteLine(ex.GetBaseException().ToString())
return 0


</CODE

3) This method fills the text box controls with the data from data gri
<CODE
private void SetValues(int nID

tr

// use some method to get the values from the db, for example like thi
//System.Collections.ArrayList arlValues = this.m_obj.GetDetails(nID)

this.txtValue.Text = arlValues[i++].ToString()
this.txtInterval.Text = arlValues[i++].ToString()
this.txtId.Text = arlValues.ToString()

catch(Exception ex

System.Diagnostics.Debug.WriteLine(ex.GetBaseException().ToString())


</CODE

NOTE: I have edited my original code since some part of it is not useful for you. This code will not work as it is. Please add your appl. logic to this
This logic works fine for me. Hope this will help you in starting with your requirement
Good Luck

CN
 
A

Aaron

All of this code is great! I really appreciate the help!

A few questions:

It appears the GetSelectedRecordID function is using the last column in my
query as the recordID Column. Is this the case? If so, would this code
still work with an SQL view made up of several tables? I am pretty new to
development and I am a little unsure of my code reading in this case.
 
A

Aaron

OK, I answered my own question for that one, I just needed to include the
activity_ID in the view that I was querying, so that works fine.

I don't know if I'll have the same luck with the next question, though.

This is in regard to the SetValues Sub.

I added two textboxes on my form for testing and called them txtFullname,
and txtBPhone.

Here is the modified SetValues code:


Private Sub SetValues(ByVal nID As Integer)
Try
' use some method to get the values from the db, for example
like this
'System.Collections.ArrayList arlValues =
this.m_obj.GetDetails(nID);

Me.txtFullname.Text = "" 'arlValues(i + 1)
Me.txtBPhone.Text = "" 'arlValues(i + 1)
'Me.txtId.Text = arlValues(i).ToString()
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex.GetBaseException
().ToString())
End Try
End Sub

I have no idea where to begin implementing this code (aside from the
textboxes). Could someone give me a little push?

Thanks again,
Aaron
 
G

Guest

OK. Let me clarify a few things before we proceed further

1. In my case, I have a primary key field called ID. When I click on a row in the datagrid, GetSelectedRecordID() will return the value of ID column for the selected row in the datagrid.

2. Using that ID, I would retrieve the values for that ID using the GetDetails(). This would return an array list of all the field's values in the table for the given ID ( in your case, FullName and BPhone for the given ID). I did not put the GetDetails() code because, I am using an XML file instead of SQL database. So, you have to write a small method that does the same like getting the values of FullName and BPhone for a given ID

3. Then I traverse the arraylist and set each value in the respective text box controls. For example like this

txtFullName.text = arlValues[0]
txtBPhone.text = arlValues[1]

Hope this helps. Feel free to ask me for any further clarfications..
Good Luck

CN
 
A

Aaron

I appreciate the insight. I'll check out the array list and see where
that takes me.

One quick question: Is the primary key you mentioned applied by your XML
file (Or in my case, the primary key in the table I am using), or is that
a primary key applied by your VB.NET dataset? I thought I saw something
about applying a primary key with SQLDataAdapter.FillSchema(), so I'm not
sure if the Primary Key in my SQL table will work or if I need to use a
specific row number from my dataset. I hope that I'm making this clear.

Thanks for all of your help!


Aaron
 
G

Guest

The primary key in my case is the applied by the XML. When I add a record to my XML file, I manually increment the ID value by 1. Over all, it is like an auto-increment field. I think your primary key would serve the purpose, since it is only to uniquely identify a row.

I don't have much knowledge about ADO.NET and other related things. So, I can't help you about SQLAdapter.FillSchema(). Sorry

HTH. Good Luck

CNU
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top