private void btnSearchDB_Click(object sender, EventArgs e)
{
OleDbConnection accessConnect = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\ECM\ECM\ECM\ECM.mdb");
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM ECMeasurements WHERE [Job Number] LIKE " + txtJobNumber.Text, accessConnect);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
There are also choices that can be searched by Date.....(txtDate.Text) and a comboBox (cbAlloyyTemper.Text). Do I write three different queries or can all the search criteria be together?
First: never use strings in the queries, but use Parameters.
Second: I would create 3 different queries, for easy debugging after. But all depends how much queries you can have, and your own comfort feeling.
This is not the way I would do this at all.
If you are looking at the same table each time you generate each query then you don't want to be querying the database each time. Instead you should load the data into a Data Table, and then query the Data Table in memory rather than the Database each time.
You need to create a Binding Source, and then set the data source for this object to your Data Table object.
For you gridView, you set the datasource for this object to your Binding Source.
Like so :-
OleDbConnection accessConnect = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\ECM\ECM\ECM\ECM.mdb");
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM ECMeasurements", accessConnect);
da.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
Now, any change in the state of the datatable reflects through to your gridview automatically.
To Query the DataTable you can use a DataView. DataView's allow you to search the Datatable with parameters like an SQL Statment. The syntax is off the top of my head, but its the method that you need to look at.
Look at Querying DataTables, using DataViews. There are many examples on the Internet.
In your scenario, its best suited.
Good Luck.
Related
I want to sort my datagrid view column which is non data bound. so please help me . following is the code
private void dgvDailyEntry_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
this.dgvDailyEntry.Rows[e.RowIndex].Cells["sno"].Value = (e.RowIndex + 1).ToString();
}
sort
Thanks in Advance
If it's not bound, I'd assume you have a datatable assigned as datasource. If you can sort the data while obtaining the datatable, the best is to sort it right there. If you can't (i.e. data are generated in random order), sort the datatable then.
C#
Datatable dt; // that is the source datatable
DataView SortedDataView = new DataView();
SortedDataView = dt.DefaultView;
SortedDataView.Sort = "COlumnNameToSortBy DESC";
dt = SortedDataView.ToTable();
VB.NET:
Dim dt as Datatable ' that is the source datatable
Dim SortedDataView As New DataView
SortedDataView = dt.DefaultView
SortedDataView.Sort = "COlumnNameToSortBy DESC"
dt = SortedDataView.ToTable()
If you somehow don't have datatable, I'd certainly recommend to implement it. It's a good practice even if you don't need sorting and such.
I searched the web and Stack Overflow and found lots of descriptions on how to fill a DataGridView with the content of a DataTable. But still it does not work for me. My DataGridView shows the correct number of columns and rows, but they appear empty.
I use following method:
public void ShowDataInGrid(ref DataTable table)
{
BindingSource sBind = new BindingSource();
dbView.Columns.Clear();
dbView.AutoGenerateColumns = false;
sBind.DataSource = table;
dbView.DataSource = sBind; //Add table to DataGridView
dbView.Columns.Add("Date", "Date");
}
Before this I created a DataGridView of name "dbView" via the designer. I am not even sure, whether I need sBind. Without it I can bind the table directly to dbView, with the same bad result.
I suspect my table is the problem. It origins from a database (SQLite) and has several columns and rows (one of the columns has the name "Date"). It is definately filled with readable data.
I mainly read the table in using following commands (after this I manipulate the data in several different steps, like changing strings and adding numbers...):
string sql = "select * from Bank";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
table.Load(reader);
reader.Close();
table.AcceptChanges();
I think the problem might be, that the table entries are stored as objects and not as string, and hence can't be shown. That's why I tried to force the content to be strings with the following change to my table:
DataTable dbTableClone = new DataTable();
dbTableClone.Load(reader);
SQLiteDataReader reader.Close();
dbTableClone.AcceptChanges();
string[] dBHeader = new string[dbTableClone.Columns.Count];
dBHeader = ReadHeaderFromDataTable(dbTableClone); //own funktion, which reads the header
DataTable table;
table.Clear();
//will first create dbTable as empty clone, so I can set DataTyp of each Column
table = dbTableClone.Clone();
for (int col = 0; col > dBHeader.Length; col++) //first set all columns as string
{
dbTable.Columns[col].DataType = typeof(string);
}
foreach (DataRow Row in dbTableClone.Rows)
{
dbTable.ImportRow(Row);
}
This did not help me neither.
Another idea: I found some comments on similar problems, where it got apparently solved with quote: "I designed columns in the VS datagridview designer. Not the column name, but the column DataPropertyName must match with fields in database." Unfortunately I don't seem to be able to do/understand this.
Following you see one row of my input table.
Try fetching and setting to GridView this way
SqlLiteConnection con = new SqlLiteConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=DB.mdf;Integrated Security=True");
con.Open();
SqlLiteDataAdapter adap = new SqlLiteDataAdapter("select * from Bank", con);
DataSet ds = new System.Data.DataSet();
adap.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
Comment everything you've done so far, try this and let me know if this works for you or not. Change connection according to your DB.
I solved the problem.
The DataTable was fine. The problem was the setup of my DataGridView dbView. I set up dbView in the designer and somehow gave it a datasource. Now I set the datasource to "none" (In "DataGridView Tasks") and my data appears as intended.
Thanks to M Adeel Khalid for looking at my stuff. Him assuring to me that my code for the link was right, made me find the solution eventually.
At the end I really only needed to use a single line:
dbView.DataSource = table;
I'm having a Data Table who gets values from some excel file. I use button as File dialog, find file somewhere in file system and then I parse needed values there.
Later in application I'm gonna need just one column from that Data Table. It is column named max t on picture attached.
Below is the code I'm using to get values from file:
string pathConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection connection = new OleDbConnection(pathConnection);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [DAYTIME CONFORT INDEX$]", connection);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
myDataGridView.DataSource = dt;
As you can see on picture I have few columns but only need some of them. For example I need to get values from column max t into some list of doubles.
I've tried few things but non of them didn't worked. Since I'm a beginner with this can someone help me with easiest way to do this.
Thank you. :)
List columns you need in query. E.g. getting only columns M and max t:
"Select [M],[max t] from [DAYTIME CONFORT INDEX$]"
This will give you an array values from the max t Column from your dataTable
DataView view = new DataView(dt);
DataTable distinctValues = view.ToTable(true, "max t");
DataRow[] myRows = distinctValues.Select();
I have a xml file in my project. I am reading the file through the below code in to combobox cbProduct. The problem is that the cbProduct is displaying duplicate text values. How to make them distinct?
I have gone through some links but there way of approach is not related to dataset.
I implemented the below code:
DataSet ds = new DataSet();
ds.ReadXml(#"..\..\stock.xml");
cbProduct.DataSource = ds.Tables[0];
cbProduct.DisplayMember = "productname";
optional: If you have time it will be appreciable if you explain the process because I am new to .net or provide a link atleast to refer (not msdn).
Please Help.
Thanks in advance.
Do this
DataSet ds = new DataSet();
ds.ReadXml(#"..\..\stock.xml");
DataTable dt = ds.Tables[0].DefaultView.ToTable(true, "productname");
cbProduct.DataSource = dt;
cbProduct.DisplayMember = "productname";
Third code line creates a new table which will have distinct values based on productname column. For More read this
This code is here
You can bring distinct values from database or you can get distinct values from c# data table into new c# data table and bind it to dropdown. How to select distinct value.
I want a user to have different "views" of a TableAdapter. These do not need to be committed back to the database.
I have tried something like the following
mytba.DataView = someOtherDataView;
with no success.
Is it possible to do this?
You need to create a new DataView from the underlying DataTable.
It's been a while since I used TableAdapters, but you can fill a data table with it
DataTable dt = new DataTable();
MyTableAdapter.Fill(dt);
DataView dv1 = new DataView(dt);
DataView dv2 = new DataView(dt);