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
Related
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 a DataGridView whose DataSource is bound to a DataView with a table bound to it. If need to be able to insert columns into the table so I just do this:
DataView.Table.Columns.Add(newColumn);
DataView.Table.Columns[columnCount-1].SetOrdinal(desiredIndex);
Trouble is after doing so the DataGridView reflect the change unless I do something silly like this.
DataView.Table = new DataTable("tempTable");
DataView.Table = orginalTable;
Wondering how to properly get the DataGridView to see the index change and redraw itself?
Update() and Refresh() did not work for me.
So I decided to go the following way:
var temp = dataGridView1.DataSource;
dataGridView1.DataSource = null;
dataGridView1.DataSource = temp;
At the moment I bind my datagridview in a following manner
relations = new CalculationsDataRelations();
bs = new BindingSource();
bs.DataSource = relations.Relations;
DgvRelations.DataSource = bs;
DgvRelations.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
DgvRelations.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
The potential problem I see here is what would happen if I change colums order or insert a column in relations.Relations object.
Is it possible to avoid situations like this and set properties of columns not by index ?
You can access columns by names. This will still be a subject to bugs if you change column names, but probablity is much lower.
you can get the right columns index using the column name of the datasource
Method to Find GridView Column Index by Name
DataTable dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Columns.Add("col4");
dt.Columns.Add("col5");
dataGridView1.DataSource = dt;
dataGridView1.Columns.RemoveAt(3);
dataGridView1.Columns.RemoveAt(2);
dataGridView1.Columns.RemoveAt(0);
In this program, I created a DataTable with 5 columns. This will be the DataSource of the DataGridView. Some columns in the DataTable doesn't need to be seen by the user, but will be used by the program later on.
After the last line, the columns are arranged as : col2, col5, col1, col3, col4. Why does it appear like this? Shouldn't it be removed from the DataGridView? What should I do to make it appear as "col2, col5"?
EDIT: I want to remove some Columns from the DataGridView, yet still be available in the DataTable. Also, it works inside an event (like Button_Click)
EDIT: I still haven't figured out why this is happening. I have no choice but to create another thread (BackgroundWorker) to do this...
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
DataTable dt = new DataTable();
int i;
dataGridView1.Invoke((MethodInvoker)delegate
{
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Columns.Add("col4");
dt.Columns.Add("col5");
dataGridView1.DataSource = dt;
dataGridView1.Columns.RemoveAt(3);
dataGridView1.Columns.RemoveAt(2);
dataGridView1.Columns.RemoveAt(0);
});
}
As far as I can tell, Haris Hasan was right; that the Form containing the DataGridView should appear at least once. But I'm still confused why...
As far as I know you will have to display DataGridView once before you can remove any column. Try this by displaying DataGridView one time and then delete the columns and see what happens
Instead of removing the columns you could try setting their visibility...
dataGridView1.DataSource = dt;
dataGridView1.Columns[3].Visible = false;
dataGridView1.Columns[2].Visible = false;
dataGridView1.Columns[0].Visible = false;
You can use DisplayIndex property of the DataGridView to order the columns. In order to hide a column you can set false to the Visible property of a particular column.
dataGridView1.Columns["ColumnTobeHided"].Visible = false;
If you don't want columns in the grid for all of the columns in the table, set datagridview.AutoGenerateColumns to false before binding the datatable to the datasource. Then create and bind the datagridview columns you actually want to have.
AutoGenerateColumns defaults to true, and yet it very seldom gives us what we really want. Take the time to build your datagridview columns exactly as you want and don't trust the default processing.
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.