I am trying to fetch data from DB and loading the DB with the values fetched. Later I am sorting the Data in the Table based on Name, then I want to bind it with a CheckedListBox
However, when I am trying to use the DataView class as shown in the code below, I see a flickering occuring while the displaying data and once all data is sorted, then it becomes stable.
How to get rid of the flickering? In the sense, I want to SORT all the data and display all of them at once.??
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
myTableForCBL.Rows.Add(myReader["Name"],myReader["rollNumber"]);
}
DataView view = myTableForCBL.DefaultView;
view.Sort = "Name";
checkedListBox1.DataSource = myTableForCBL; //datatable for checked list box
checkedListBox1.DisplayMember = "Name";
checkedListBox1.ValueMember = "rollNumber";
This is because I am sorting the way the data is shown and not merely the data itself. So, it flickers. Rather than that, sort the Data in the DataTable using the
DataRow[] rows = DataTable.select(filterexpression, sortVariable);
//make sure the filterexpression="", to select all rows else if you want certain rows, write a filterexpression that does the filtering.
//sortVariable to be a column of the DataTable
then import the rows to another temp table and source that to the CheckedListBox
Then the flickering or flashing of the Data Stops.
Related
I have to display data in a grid binded with binding list
BindingList<ExecutionSummaryData> ExecutionList
grid.DataSource = ExecutionList;
where ExecutionSummaryData contains property like Name, Age, Address etc(for example).
Now through a winform when user select certain name, age or other parameters i have to select data from ExecutionList and have to bind the filtered data to my grid.
Can I somehow bind this query to ExecutionList so that each time query string changed ExecutionList will get binded to grid and display filtered data?
Can I bind any sublist, retrieved from ExecutionList through filter string, to my grid and for each filter query change it will fetch data from ExecutionList and display accordingly.
I don't want to clear and refill data to ExecutionList on each filter as this grid will be getting updated real time with around 10-20 million records.
Editing my requirement
Please suggest how what will be the syntax of Linq query or Lambda expression over this ExecutionList to fetch filtered record .
Filter query will be including condition with multiple entities to compare and fetch from list
like "
Select * from ExecutionList where name in('N1','N2','N3',,,,'N90')"
AND AGE in ('A1','A2','A3',,,,'A90')
Please suggest as how can i achieve this? Based on replies I will be updating my question with missing/required info needed.
Thanks,
Ashish
if you want to filter your data from a DataTable i would suggest using a DataView
DataView dv = new DataView();
dv = new DataView(parameterDs.Tables[0], "ParameterName = '#" + parameter.Key + "'",string.Empty, DataViewRowState.CurrentRows);
you can also use sort on DataView
dv.Sort = "Name";
and then just use
grid.DataSource = dv;
Another way to use DataView is by assigning a query to it as below:
DataTable contacts = dataSet.Tables["Contact"];
EnumerableRowCollection<DataRow> query = from contact in contacts.AsEnumerable()
where contact.Field<string>("LastName").StartsWith("S")
orderby contact.Field<string>("LastName"), contact.Field<string>("FirstName")
select contact;
DataView view = query.AsDataView();
bindingSource1.DataSource = view;
dataGridView1.AutoResizeColumns();
or by using the DataView 'RowFilter' Property:
DataTable contacts = dataSet.Tables["Contact"];
DataView view = contacts.AsDataView();
view.RowFilter = "LastName='Zhu'";
bindingSource1.DataSource = view;
dataGridView1.AutoResizeColumns();
and whenever you want to clear your filter just write:
view.RowFilter = null; //or String.Empty
you might want to take a look at the examples here and here:
Create a stored procedure with paging and pass your search filter and retrieve date in few thousand. Or you can IQueryable so this will fire query based on your filter and will retrieve the data. "This is a comment, not an answer."
I have a table on the db, which is shown on 2 different datagridviews, but, one is filled by default, showing all the data on the table, and the second one I want to be filled with a condition.
So I'd like to fill that table initialy with a custom select query. How can I achieve that?
pd: I use datasets, in which I created the custom query, so the question should be how do I fill by default a table with certain tableadapter I made.
if you can share your codes, you will find a most right answer quickly.
var query = db.table.Where(x=>x.column1==condition1).ToList();
dataGridView1.DataSource = query (); dataGridView1.DataBind();
Or, if you have filled datatable/dataset you can write
dataGridView1.DataSource = yourDataTable; dataGridView1.DataBind();
You want to filter the data in your DataSet then set the DataSource of your DataGridView to a DataTable created by that filter. The code would look something like:
// Need to create a DataView from your original DataSet
// and set the view to the default view from your DataSet.
DataView view = new DataView();
view = myDataSet.Tables[0].DefaultView;
// Next, create a filter on that view
view.RowFilter = "State = 'CA'";
// Now create a DataTable from the view
// and set the DataSource of the DataGridView to that DataTable.
DataTable dt = view.ToTable("tablename");
dgrdToDisplay.DataSource = dt;
I have MySQL database and a DataGridView in C# and to fill the DataGridView I do the following:
schoolDataSet schl = new schoolDataSet();
schoolDataSetTableAdapters.studentinfoTableAdapter adptr = new schoolDataSetTableAdapters.studentinfoTableAdapter();
adptr.Fill(schl.studentinfo);
dataGridView1.DataSource = schl.studentinfo.DefaultView;
and undesired columns I make them visible = false from DataGridView properties but I came with a problem if I want to specify what data (rows) to fill in DataGridView such applying a where condition like:
fill data in DataGridView WHERE IsActive = 1 so can I still use the above code with some modifications or I have to write SQL query and fill the DataGridView manually ?
After searching and trying tons of codes I got it as following in simplest code:
In the code above just comment out last line which is dataGridView1.DataSource = schl.studentinfo.DefaultView; or simply replace it with the following
DataView dv = new DataView(schoolDataSet.studentinfo, "IsActive = 'false'", "id", DataViewRowState.CurrentRows);
Which creates a new DataView and filters according to IsActive column with false value, the third parameter id is to sort based-on, and finally you can write another line
dataGridView1.DataSource = dv; that will tell the DataGridView to load data from DataView.
Hope to save someone's time.
Big thanks goes to #Karthik Ganesan
I have a DataGridView which I have used RowFilter on it like following:
(dgv.DataSource as DataTable).DefaultView.RowFilter = whereClause;
Before applying the RowFilter the DataGridView has 1087 records and afterwards, it has about 8.
Now I want to work on those 8 records.
How can I access them?
If you do
dgv.DataSource as DataTable
Then you can do the following
dataTable dt = dgv.DataSource as DataTable;
DataRow[] rows = dt.Select("your filter goes here");
You can do same thing by Linq if you want to. These are your rows and you can work with them. And to Edit only these records do:
DataGridView.DataSource = rows;
Also, you can create a custom DataView, which will filter records the way you want it, and set datasource to that view. But remember, the key here, no matter how many views you have - this is same dataTable that you looking at
DataView view = new DataView();
view.Table = dt;
view.RowFilter = "your filter";
view.Sort = "your sort";
You can use DataGridViewRow.DataBoundItem to access to the underlying data.
If your DataGridView bound to the DataView the underlying data is of type DataRowView. You can use code like this:
var dataRow = dgv.Rows[rowIndex].DataBoundItem as DataRowView;
I am showing a DataGridView control on my form, populating it with a DataTable object through the DataSource property of the control. I set the control to select the entire row when it is selected, and enabled multiselect. All of this works swimmingly.
A user can select multiple rows and click a button. I want to make a new DataTable object with copies of the selected rows and pass that to a Crystal Report.
Is there an efficient means of doing this? It seems the only way is to parse the new rows for the new DataTable from the selected grid cells, which strikes me as relatively ridiculous.
This is my solution to the question "How to get selected Rows as DataTable":
copy structure of original DataTable
copy each row (as a row may exist only in one datatable object)
DataTable selectedRows = (sourceDataGridView.DataSource as DataTable).Clone();
foreach(DataGridViewRow row in sourceDataGridView.SelectedRows) {
selectedRows.Rows.Add((row.DataBoundItem as DataRowView).Row.ItemArray);
}
I've found that the best way to avoid iterating over the DataTable to find the rows you want is to bind to a DataView instead. This way, you can sort and all that other fun stuff you like to do with a DataGridView, and can still get the data you need without manually parsing the underlying DataTable.
Below is a succinct example that should show you everything you need to get a DataRow without searching:
DataGridView dataGridView1 = new DataGridView();
DataTable tb = new DataTable();
DataView dv = tb.AsDataView();
dataGridView1.DataSource = dv;
// Get the row indexes in whatever your favorite manner is.
List<int> selectedRowIndexes = YourGetSelectedRowIndexesMethod();
foreach(int selectedRowIndex in selectedRowIndexes)
DataRow dr = dv[selectedRowIndex].Row;
One quick method to get the selected indexes (just in case you need it):
List<int> indexes = new List<int>();
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
indexes.Add(row.Index);
}