I am trying to create a gridview with a string column, a checkbox column, and a dropdownlist/combobox column. The first two are finished (all code behind), just need help with the last one.
DataTable dt = new DataTable("tblAir");
dt.Columns.Add("Flight Details", typeof(string));
dt.Columns.Add("Prefered Seating", typeof(bool));
//doesn't work
dt.Columns.Add("Add Remark", typeof(ComboBox));
The data for the combobox is being supplied on load as we cannot work with a database.
Peter Bromberg has a detailed article on creating a Winforms gridview with comboboxes:
http://www.eggheadcafe.com/articles/20060202.asp
DataAccessLayer dal = new DataAccessLayer();
DataTable movies = dal.GetMovies();
gvMovies.DataSource = movies;
gvMovies.AllowUserToAddRows = false;
gvMovies.AllowUserToDeleteRows = false;
//Create the new combobox column and set it's DataSource to a DataTable
DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
col.DataSource = dal.GetMovieTypes(); ;
col.ValueMember = "MovieTypeID";
col.DisplayMember = "MovieType";
col.DataPropertyName = "MovieTypeID";
//Add your new combobox column to the gridview
gvMovies.Columns.Add(col);
Related
I want to define the columns of my DataGridView manually from code. Something like this:
int index = 0;
string columnName = "something";
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.Name = columnName;
column.HeaderText = columnName;
myDataGridView.Columns.Insert(index++, column);
And then I bind the DataGridView.DataSource to a DataTable:
myDataGridView.DataSource = null;
table = new DataTable();
table = dbAccess.GetDataFromDB(); //This function returns a DataTable filled with data
myDataGridView.DataSource = table;
I already checked that after executing GetDataFromDB table is filled with data, but when binding it to DataSource, the DataGridView keeps all its row empty (although it creates as many rows as there are on the table).
Some code that I'm missing here?
I have a DataGridView (DGV) which is populated using a datatable populated by a Stored Procedure. (All DB calls are correctly returning the requisite data). I then add a combobox column to the DGV which should provide a selection of batchcodes based on the stock code of that row. (Another Stored Procedure Call passing in the StockCode) However, all combo box's on every row are populating with the combined batchcodes for all the rows. In this example I have tried creating an array of datatables to dynamically assign to the DGV.Row but it's the same result. I kind of understand why it isn't working but I don't know how to rectify it. (I am assuming that since I am not specifically defining the cell index of the current combobox and assigning it's datasource to a datatable from the array - it is just overwriting it?? Any help would be greatly appreciated.
// fill the adapter with the executed cmd
adapter.Fill(dt);
//populate the gridview with the datatable
dgv_BOM.DataSource = dt;
dgv_BOM.AutoSize = true;
dgv_BOM.AutoResizeColumns();
dgv_BOM.Columns["StockCode"].DisplayIndex = 0;
dgv_BOM.Columns["Description"].DisplayIndex = 1;
dgv_BOM.RowHeadersVisible = false;
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Select Batch Code";
cmb.Name = "cmb";
cmb.Width = 150;
dgv_BOM.Columns.Add(cmb);
DataTable[] dtArray = new DataTable[dgv_BOM.Rows.Count];
int count = 0;
foreach (DataGridViewRow row in dgv_BOM.Rows)
{
if (row.IsNewRow) continue;
stckcd = row.Cells["StockCode"].Value.ToString();
dtArray[count] = SP_RMBatchCodes(stckcd); //returns a datatable object
cmb.DataSource = dtArray[cnt];
cmb.DisplayMember = "BatchNo";
count++;
}
this.dgv_BOM.Columns["Traceable"].Visible = false;
In my WinForms app, i've three tabs and in each tab a gatagridview.
If i click a btn to display records in tab1 datagridview, it populates BUT if i go to tab2 and hit a button to display a different record, it first displays me the right records and then dispalys me the same records in tab1 or tab3 if a different button is clicked.
It then populate me the same record from tab1,tab2 and tab3.
How can i solve this problem OR is it that i declared a globale Datatable dt variable?
If you want the 3 DGVs to have each his own record pointer you must not use the DataTable as their DataSource directly. Instead use an intermediate BindingSource for each of them:
// assume a few DataGridViews..:
DataGridView DGV1 = new DataGridView();
DataGridView DGV2 = new DataGridView();
DataGridView DGV3 = new DataGridView();
// and a common DataTable:
DataTable DT = new DataTable();
//..
// we need a separate BindingSource for each DGV:
BindingSource BS1 = new BindingSource();
BindingSource BS2 = new BindingSource();
BindingSource BS3 = new BindingSource();
// each is bound to the DataTable
BS1.DataSource = DT;
BS2.DataSource = DT;
BS3.DataSource = DT;
// now we set them to be the DatSource of the DGVs:
DGV1.DataSource = BS1;
DGV2.DataSource = BS2;
DGV3.DataSource = BS3;
// now we can set the record pointers separately:
BS1.Position = 3;
BS2.Position = 0;
BS3.Position = BS3.Count - 1;
// or set filters:
BS2.Filter = "someCondition";
// or set sorts:
BS3.Filter = "someSort";
I am new to c#. I need to have a checkbox field in the "selected" column of my datagridview instead of the "False"(or "True") text that is showing currently. This datagridview is databound and the data is obtained by reading an xml file. How can I achieve this?
before writing to the xml file, this is what i did.
DataTable dtGens = new DataTable(); //creates a new Datatable object for the Gens
dtGens.TableName = "Gen Types";
DataColumn dc1 = new DataColumn("Generator");
DataColumn dc2 = new DataColumn("alpha");
DataColumn dc3 = new DataColumn("beta");
DataColumn dc4 = new DataColumn("circuit breaker");
DataColumn dc5 = new DataColumn("description");
DataColumn dc6 = new DataColumn("Selected",System.Type.GetType("System.Boolean"));
dtGens.Columns.Add(dc1); //associates the columns to the dtGens datatable
dtGens.Columns.Add(dc2);
dtGens.Columns.Add(dc3);
dtGens.Columns.Add(dc4);
dtGens.Columns.Add(dc5);
dtGens.Columns.Add(dc6);
DataRow drow;
for (int i = 0; i < 50; i++)
{
drow = dtGens.NewRow();
drow["Generator"] = "Gen " + (i + 1).ToString();
drow["alpha"] = 0.0;
drow["beta"] = 0.0;
drow["circuit breaker"] = 0.0;
drow["description"] = "myGen";
drow["Selected"] = false;
dtGens.Rows.Add(drow);
}
//creates a new DataSet Object that will help write generator data to XML
DataSet feederProject = new DataSet();
feederProject.Tables.Add(dtGens);
feederProject.WriteXml("Generators.xml");
//preview
DataSet feederProject = new DataSet();
feederProject.ReadXml("Generators.xml");
dataGridViewLoadsDGs.DataSource = feederProject.Tables[0];
A column bound to boolean type automatically shows checkboxes. The problem is that the schema information of your DataTable is lost when it's written into the XML.
To prevent that, you can use an overload of DataSet.WriteXml that takes XmlWriteMode as a parameter, which allows you an option to write the schema information.
feederProject.WriteXml("Generators.xml", XmlWriteMode.WriteSchema);
To be able to represent a bound boolean data as a check box column you need to set AutoGenerateColumns property of the DataGridView to false. Then add columns manually and for the column that must be checkbox column set a instance of DataGridViewCheckBoxColumn:
dataGridViewLoadsDGs.AutoGenerateColumns = false;
...
dataGridViewLoadsDGs.Columns.Add(new DataGridViewCheckBoxColumn());
I create a DataTable and bind it to a DataGrid. My DataSource consist of one Table (FooTable) which consist of one column (FooName).
The following codes runs fine - except that each time I add a new row, there is a duplicate Column fill in with the same data, which I don't know how to get rid of. See below image and code. I have only one FooName column and a duplicate column comes out.
/* Create a DataGrid dg1 */
DataGrid dg1 = new DataGrid();
DataGridTextColumn col = new DataGridTextColumn();
col = new DataGridTextColumn();
colA.Binding = new Binding("FooName");
colA.Header = "FooName";
dg1.Columns.Add(colA);
dataGrid1.Children.Add(dg1);
/* Create a DataTable and bind it to DataGrid */
SqlCeDataAdapter da = new SqlCeDataAdapter();
string sqlStr = #"SELECT * FROM FooTable";
da.SelectCommand = new SqlCeCommand(sqlStr, conn);
da.Fill(ds, "FooTable");
dt = ds.Tables["FooTable"];
DataRow newRow = dt.NewRow();
newRow["FooName"] = "Donkey";
dt.Rows.Add(newRow);
dg1.ItemsSource = ds.Tables[0].DefaultView;
try setting AutoGenerateColumns to false
dg1.AutoGenerateColumns = false
Try
dg1.AutoGenerateColumns = false;
Should do the work for you. For now the datagrid automatically generates the columns AND adds the one you asked