Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add new row to an unbound data grid view

Status
Not open for further replies.

darude

Programmer
Jun 23, 2003
138
0
0
US
Good Afternoon,
I have a bound data grid view. When a user double-clicks it I want the selected row to populate an unbound data grid view. This is for a survey and I want the second data grid unbound so the user can change the order of the contents before I add the data to a production table.

So, I searched the internet and found that I could create 2 columns AnswerID, AnswerText. When I run it, I get System.Windows.Forms.DataGridViewSelectedRowCollection. It's something, so I feel like I'm close. Am I wrong?

This what I have so far:
dgvSurveyAnswers = New DataGridView()
dgvSurveyAnswers.Columns.Add("AnswerID", "AnswerID")
dgvSurveyAnswers.Columns.Add("Answer_Text", "Answer_Text")

Controls.Add(dgvSurveyAnswers)
dgvSurveyAnswers.Rows(0).Cells(0).Value = dgvAnswers.answerid

Thanks in advance.
 

Create an unbound DataTable and bind the DataGridView to that. When you add records to the DataTable they will show up in the DataGridView.

Code:
Dim dtSurveyAnswers As DataTable
Dim dcAnswerID As DataColumn
dim dcAnswerText As DataColumn

dcAnswerID = New DataColumn("AnswerID")
dcAnswerText = New DataColumn("Answer_Text")

dtSurveyAnswers.Columns.Add(dcAnswerID)
dtSurveyAnswers.Columns.Add(dcAnswerText)

dgvSurveyAnswers = New DataGridView()
dgvSurveyAnswers.DataSource = dtSurveyAnswers

Controls.Add(dgvSurveyAnswers)


To add a row to the DataTable:

Code:
Dim dr As DataRow

dr = dtSurveyAnswers.NewRow

dr.Item("AnswerID") = dgvAnswers.answerid
dr.Item("Answer_Text") = "Some text goes here"

dtSueveyAnswers.Rows.Add(dr)

dgvSurveyAnswers.Refresh()

Hope this helps.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top