I have a cbolist the user will select a name hit enter then a query runs to get that name details and enters it to a grid view, the user clicks another name and its details are added.
How do i do this. i have only used the adapter.fill(table)
gridview1.datasource = table
but only one record is showing the last one clicked.

adding single records to a gridview
thelonesoldier
You can do this also instead of creating table from scratch;
therecords = table.Copy() // do this when the table is still empty
To Load
If using a Shallow Copy
threcords.LoadDataRowFrom(table.Rows(0)) // double check the LoadDataRowFrom, it might be the exact name
Your current approach
therecords.LoadDataRowFrom(new Object() {table.Row[0]Col("col1"), ... , ...})
Satyanand
yep i figured it out with some trial and error I had to remove the newobject() though it was treating it as a system object and trying to put it in to my table as a col value
Thanks
Martin Kristensen
well from the impression I am getting, you are only retrieving 1 record at a time from SQL/your database based on the name they chose from the cbolist correct if so then well, you will only get 1 record back if a match was found.
on another note, filling a dataset and binding it to a datagrid for just 1 record is a bit overkill/expensive. I would suggest using a DataReader if you are just going to be retrieving such a small number of records (which are managable)
are you able to post some code as well as explain a bit more in depth of the problem
Dallastower
well its not going to work exactly like that.
everytime you fill a dataset it will I believe clear out the existing entries and fill it with new data and this is what you are binding to the datagrid.
instead you may wish to use a SqlDataReader, read the data and add it into a listbox perhaps, or even create a datatable locally and add values to that and bind it to the datagridview.
Example:
Dim theRecords as new DataTable() 'global
...
...
'method should only be called once to create the datatable:
private sub DoCreateDataTable()
Me.theRecords = new DataTable()
Me.theRecords.Columns.Add("columnName")
end sub
private sub DoGetRecord(byval itemChosen as String)
Dim theSQLCommand as new SqlCommand("SELECT [fieldName] FROM [tableName] WHERE [fieldName] = '" & itemChosen & "'", new SqlConnection(connectionStringHere))
try
theSQLCommand.Connection.Open()
Dim theDataReader as SqlDataReader = theSQLCommand.ExecuteReader(CommandBehavior.CloseConnection)
while theDataReader.Read()
Me.theDataTable.Rows.Add(new object() { theDataReader(ColumnName).ToString() })
end while
theSQLCommand.Connection.Close()
Me.theDataGrid.DataSource = Me.theRecords.DefaultView
catch ex as SqlException
'handle exception
end try
end sub
does this help
well it will create a new instance of a datatable - where we will store our records. We add a column to it and we will only be adding a record and 1 column per record
we then execute the command to fetch data from the database based on the selecteditem. It will then use a SqlDataReader to read the data we recieve by executing that query and getting the results returned. Once it has been returned, it will then add that column of result (row by row until no more rows) into the datatable we created. We then bind this to our dataGrid once completed
IGiberson
JRQ - you could do that but its still expensive just to get 1 record at time using a SqlDataAdapter and filling a dataset. I would strongly advise to use the SqlDataReader since its a fast forward only datareader and doesnt care about anything else or that has the overhead of a dataAdapter. :-) performance and memory is the key
should it have been fetching a good number of records then yes, your idea would be the way. it can still be implemented no doubt but It should be focused on the "long" term or to make sure the application is better now rather than later, if that makes sense.
DeepNight
yes there will be one record returned each time the hit enter. they will be adding 10, + records. They do not want to have a multiple select list box. so i need to add one record to the gridveiw at a time.
Batkuip
hariarla
You will have to create a "shallow" copy of the table which you will use as the datasource then add the row to the copy by loading the content of the row from the table that you use for query.
NOTE: You can use your shallow copy to update the database just pass it to the data adapter.
RubenPieters
good catch. I fixed it now, thanks
in regards to reading the other columns....you need to alter the SELECT statement to the names of fields you want to return back or do a * for all records.
SELECT * FROM [tableName] WHERE [fieldName] = value
so then to read it, we need to add it into our rows collection. We do the same thing but read the specific columns....
therecords.Rows.Add(New Object() { theDataReader("job code").ToString(), theDataReader("Last Name").ToString(), theDataReader("First Name").ToString(), theDataReader("Status").ToString() })
and so on
does this make sense
Pure Krome
renyx
thanks that was my thought but I was running into problems (new to .net)
I am gettng errors on the
therecords.Rows.Add(
New Object(), theDataReader("job code").ToString())(I am using vb.net) not c#
and how would I add mutipler col. I under stand this much but not how to add each value from my query
therecords = New DataTable()
therecords.Columns.Add("Job Code")
therecords.Columns.Add("Last Name")
therecords.Columns.Add("First Name")
therecords.Columns.Add("Status")
therecords.Columns.Add("SOC#Sec#")
therecords.Columns.Add("Department")
therecords.Columns.Add("Dept Descr")
therecords.Columns.Add("exitdt")