how to add sqldatasource to gridview which is having data without override? - c#

I am new to .Net application development. I'm working on a project using MS Access.
In my application I have one Gridview with unbound data. I have one SQL Datasource which I bind to Gridview dynamically. When I bind that SQL Datasource to the Gridview the existing data is overridden. Instead of overriding I want to append the rows to the existing rows in Gridview.

You can't do that. Best fix would be not to use SqlDataSource and get data from db, merge it with existing and bind it to grid.

That was doing the right thing; It definitely overrides the previous data. In that case, you need to create a dataset and append the newly fetched table data to the existing table in the dataset.. :)

You can't.
Gridview can have only one datasource at time. Every time you try to bind another datasource, it always overrides the existing one.
One of the best option is to use JOIN if these datasource are in same database.
Check also:
ASP.NET tips: Display resultset from Multiple DataTable

create a view with multiple data datable
a sample code is below
jv = New JoinView(ds.Tables!Ord, _
"OrderID,CustomerID,EmployeeID,OrderDate,CustOrd.CompanyName Company,CustOrd.ContactName Contact,CustOrd.ContactTitle Position,EmpOrd.FirstName,EmpOrd.LastName", _
"EmployeeID<5", "OrderDate")
DataGrid1.DataMember = ""
DataGrid1.DataSource = jv
for more info go through
http://support.microsoft.com/default.aspx?scid=kb;EN-US;325682

Related

Added column to database, bound gridview not updating C#/winforms/SQL

I've set up a simple data bound gridview that is populated via the autogenerated code for winforms. It is filling based off of the dataset I point it at.
I've updated the underlying database it's supposed to be filling off of to add an additional column to a table. However this added column is not appearing in the gridview.
I have deleted the datset and rebound it and can't find an answer online but am probably searching with incorrect terms. Is there a way to refresh the dataset in some way?
How the gridview is being filled is by:
this.xTableAdapter.Fill(this.DBDataSet.tableName);
I imagine there is a simple way to refresh the underlying dataset but cannot for the life of me find what it is.
This link had the answer. I needed to go into the dataset designer view and right click on the get, fill() query in the query area for my table. Doing so allowed me to modify the query that built the dataset, adding in the missing column.

C# : Resetting DataGridView for new DataTable

Ok so I'm trying to make a simple SQL CE Viewer application just to view my local databases for another application, I have it setup so that I can select what database I want to open and then it automatically populates a combo box with all of the tables in the database. When I select a table it populates a DataGridView with the records in the table.
My problem is switching between tables. I can't seem to get the DataGridView to remove everything from the previous table and re-populate the DataGridView with the new table information. Of course each table has different columns and rows and such.
I've googled and searched on here and every suggestion I find doesn't seem to work. It populates the DataGridView with the first table just fine, but when I select another it basically adds the columns and rows into whatever was there....
How can I get the DataGridView to completely clear for new data?
And please don't tell me to use dataGridView1.DataSource = null; tried that, doesn't work.
Well, I checked it in one of my programs and just changing DataSource property is working fine. I didn't have to use datagridview.Refresh(). Maybe it depends on the kind of DataSource, which you are using to set datagridview data?
Write this line after you have done populating the DataSource with the new data
dataGridView1.Refresh();
You don't need to clear the DataGridView directly. Its always handled by modifying the DataSource of the DataGridView.
If there is nothing else in the form, you can simply call InitializeComponent() again. But I think #Josh is right.

Paging in Datagridview without using dataadapter c#

I want to add paging option for my datagridview. I have a datatable that fetch records from the xml file and bind it in datagridview. Xml contains so many records that are why I want to add paging option in datagridview in C #win forms.
Please help how I can achieve this task without using data adapter.
Thanks.
Page it manually. Create another DataTable with same scheme and copy one page of rows to it, at the times you require.
Of course, that is one option for doing it.

What is the easiest way to display a table of data?

Coming from PHP background. What would be the easiest (best?) way to display a table of data populated in C#? The format of the data could be modified as required, right now, I am thinking 2D list to store the data.
Gridview is nice and all but I haven't found a way to use a C# List as it's data source.
No real feature is required. I can implement my own editor / inserter, etc.
Gridview is what you need. To bind to a list, you just set the list as the datasource and away you go:
List<x> myList = GetList();
myGrid.DataSource = myList;
myGrid.DataBind();
Or use an ObjectDataSource control on the page instead...
DataTable could be another best option
as it can support the specific columns of the table or you can manage the whole table in the DataTable
DataTable dt=new DataTable();
now you can store your table from the database into this and can use it as a data source for your listview or gridview

Get connection to DataTable object

I have C# application that fills a GridView with data from a DataTable object, which is filled from parsing a text file. Now i want to do a master-detail scheme, where the master GridView shows an identifying column from the DataTable for all rows, and the detail GridView shows all the columns for that row.
All the examples I'm finding seem to require a SqlDataSource control (to provide the filtering mechanism), which then requires a connection to a database. I can't find how to provide this information when using only a DataTable data source. What provider would I use, and what would the connection string look like?
Thanks in Advance for any help.
You already have your DataSource (unless the data will change when you click a GridViewRow), so all you need is to get something like a Key from your current GridView, create a new DataSource by loading data from your DataTable with the selected key in your GridView and bind it to your new GridView. I could also suggest that you take a look to the DetailsView control which is designed to do in fact what you are trying to accomplish here (master-details scheme).
Good luck my friend.
No connection string needed. You already have the data in a DataTable, so you should be able to do something along the lines of:
myGridView.DataSource = myDataTable;
myGridView.Databind();

Categories