Binding GridViewComboBoxColumn to a datasource - c#

I already know how to specify the datasource but afther doing that it is not populated yet so i was thinking you need some kind of bind() command to populate the comboboxcolumn in the edit form
This below is how i bind the datasource to the comboboxcolumn (and yes i am sure that ds has data rows in it)
(ASPxGridView4.Columns["Naam"] as GridViewDataComboBoxColumn).PropertiesComboBox.DataSource = ds as DataSet;
So could anyone tell me how i can now populate the comboboxcolumn in edit mode?
Edit
protected void ASPxGridView4_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
{
if (dt.Rows.Count < 1)
{
ds = Session["ds"] as DataSet;
}
GridViewDataComboBoxColumn column = (ASPxGridView4.Columns["Naam"] as GridViewDataComboBoxColumn);
column.PropertiesComboBox.DataSource = ds.Tables[0];
column.PropertiesComboBox.ValueField = "Naam";
column.PropertiesComboBox.ValueType = typeof(string);
column.PropertiesComboBox.TextField = "Naam";
}

Here is the code which should work:
DataSet dataSet = ds as DataSet;
GridViewDataComboBoxColumn column = (ASPxGridView4.Columns["Naam"] as GridViewDataComboBoxColumn);
column.PropertiesComboBox.DataSource = dataSet.Tables[0];
column.PropertiesComboBox.ValueField = "SomeValueField";
column.PropertiesComboBox.ValueType = typeof(int); // type of the SomeValueField
column.PropertiesComboBox.TextField = "SomeTextField";
Also, please refer to the GridViewDataComboBoxColumn Class topic.
UPDATE Your code should be implemented in the CellEditorInitialize event as shown below:
protected void ASPxGridView1_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) {
if(e.Editor is ASPxComboBox) {
ASPxComboBox combo = ((ASPxComboBox)e.Editor);
combo.DataSource = dataSet.Tables[0];
combo.TextField = "Naam";
combo.ValueField = "Naam";
combo.DataBindItems();
}
}

Related

Why a data grid view is not being updated based on data set connected with bindig source?

I have a dataGridView control with data source set at dataTableDocumentsBindingSource
The dataGridView is not being updated when i am updating dataSet1 with the following code:
DataSet1 ds = new DataSet1();
/.../
DataTable documentsTable = ds.Tables["DataTableDocuments"];
DataRow workRow = documentsTable.NewRow();
workRow["id"] = Int32.Parse(item.SelectSingleNode("id").InnerText);
workRow["Title"] = item.SelectSingleNode("title").InnerText;
documentsTable.Rows.Add(workRow);
/.../
return ds;
How to get dataGridView to update when respective dataSet is being updated?
I have been already trying dataGridView.Refresh() and dataGridView.Update() but neither works.
If you could do it like this
public class Xyz
{
DataSet1 ds = new DataSet1();
/.../
private void UpdateData()
{
DataTable documentsTable = ds.Tables["DataTableDocuments"];
DataRow workRow = documentsTable.NewRow();
workRow["id"] = Int32.Parse(item.SelectSingleNode("id").InnerText);
workRow["Title"] = item.SelectSingleNode("title").InnerText;
documentsTable.Rows.Add(workRow);
yourbindingsource.ResetBindings(false);
}
}

DataGridView doesn't update when assigning datasource of second datagrid

I have two dataGridViews but only one displayed.
They both have exactly the same columns but different values.
The problem is that only one is displayed and when I select the first or second dataGridView, my dataGridView that was showing someting, stops and doesn't work at all anymore.
I tried this
dataGridView1 = DataGridView2;
Like this
BindingSource b = new BindingSource();
b.DataSource = dataGridView2.DataSource;
dataGridView1.DataSource = null;
dataGridView1.DataSource = b;
dataGridView1.Visible = true;
dataGridView1.AutoGenerateColumns = true;
But nothing is working... And yes, I tried Update() and Refresh()
You can verify your application with help of following simple Example
namespace GridView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataSet ds= new DataSet();
ds.ReadXml(#"C:\Users\user\Desktop\students.xml");
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "student";
Application.DoEvents();
}
private void Form1_Load(object sender, EventArgs e)
{
BindingSource b = new BindingSource();
dataGridView2.AutoGenerateColumns = true;
b.DataSource = dataGridView1.DataSource;
dataGridView2.DataMember = "student";
dataGridView2.DataSource = b;
}
}
}
I think you miss the DataMember attribute I assign the datasource of grid 1 to grid 2 and this works for me also.

how to bind datatable to datagridview in c#

I need to bind my DataTable to my DataGridView.
i do this:
DTable = new DataTable();
SBind = new BindingSource();
//ServersTable - DataGridView
for (int i = 0; i < ServersTable.ColumnCount; ++i)
{
DTable.Columns.Add(new DataColumn(ServersTable.Columns[i].Name));
}
for (int i = 0; i < Apps.Count; ++i)
{
DataRow r = DTable.NewRow();
r.BeginEdit();
foreach (DataColumn c in DTable.Columns)
{
r[c.ColumnName] = //writing values
}
r.EndEdit();
DTable.Rows.Add(r);
}
SBind.DataSource = DTable;
ServersTable.DataSource = SBind;
But all i got is DataTable ADDS NEW columns to my DataGridView.
I don't need this, i just need to write under existing columns.
Try this:
ServersTable.Columns.Clear();
ServersTable.DataSource = SBind;
If you don't want to clear all the existing columns, you have to set DataPropertyName for each existing column like this:
for (int i = 0; i < ServersTable.ColumnCount; ++i) {
DTable.Columns.Add(new DataColumn(ServersTable.Columns[i].Name));
ServersTable.Columns[i].DataPropertyName = ServersTable.Columns[i].Name;
}
Even better:
DataTable DTable = new DataTable();
BindingSource SBind = new BindingSource();
SBind.DataSource = DTable;
DataGridView ServersTable = new DataGridView();
ServersTable.AutoGenerateColumns = false;
ServersTable.DataSource = DTable;
ServersTable.DataSource = SBind;
ServersTable.Refresh();
You're telling the bindable source that it's bound to the DataTable, in-turn you need to tell your DataGridView not to auto-generate columns, so it will only pull the data in for the columns you've manually input into the control... lastly refresh the control to update the databind.
On the DataGridView, set the DataPropertyName of the columns to your column names of your DataTable.
// I built my datatable first, and populated it, columns, rows and all.
//Then, once the datatable is functional, do the following to bind it to the DGV. NOTE: the DGV's AutoGenerateColumns property must be 'true' for this example, or the "assigning" of column names from datatable to dgv will not work. I also "added" my datatable to a dataset previously, but I don't think that is necessary.
BindingSource SBind = new BindingSource();
SBind.DataSource = dtSourceData;
ADGView1.AutoGenerateColumns = true; //must be "true" here
ADGView1.Columns.Clear();
ADGView1.DataSource = SBind;
//set DGV's column names and headings from the Datatable properties
for (int i = 0; i < ADGView1.Columns.Count; i++)
{
ADGView1.Columns[i].DataPropertyName = dtSourceData.Columns[i].ColumnName;
ADGView1.Columns[i].HeaderText = dtSourceData.Columns[i].Caption;
}
ADGView1.Enabled = true;
ADGView1.Refresh();
foreach (DictionaryEntry entry in Hashtable)
{
datagridviewTZ.Rows.Add(entry.Key.ToString(), entry.Value.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable StudentDataTable = new DataTable("Student");
//perform this on the Load Event of the form
private void AddColumns()
{
StudentDataTable.Columns.Add("First_Int_Column", typeof(int));
StudentDataTable.Columns.Add("Second_String_Column", typeof(String));
this.dataGridViewDisplay.DataSource = StudentDataTable;
}
}
//Save_Button_Event to save the form field to the table which is then bind to the TableGridView
private void SaveForm()
{
StudentDataTable.Rows.Add(new object[] { textBoxFirst.Text, textBoxSecond.Text});
dataGridViewDisplay.DataSource = StudentDataTable;
}
for example we want to set a DataTable 'Users' to DataGridView by followig 2 steps :
step 1 - get all Users by :
public DataTable getAllUsers()
{
OracleConnection Connection = new OracleConnection(stringConnection);
Connection.ConnectionString = stringConnection;
Connection.Open();
DataSet dataSet = new DataSet();
OracleCommand cmd = new OracleCommand("semect * from Users");
cmd.CommandType = CommandType.Text;
cmd.Connection = Connection;
using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet);
}
return dataSet.Tables[0];
}
step 2- set the return result to DataGridView :
public void setTableToDgv(DataGridView DGV, DataTable table)
{
DGV.DataSource = table;
}
using example:
setTableToDgv(dgv_client,getAllUsers());

ResetBinding in C#

I have a function
public DataSet Select(string sql)
{
//Get data source from database to DataSet
}
in class DataDriverExample
and in form_load, I bind to the datagridview.
public BindingSource bs = new BindingSource()
public void form_load()
{
DataSource ds = DataDriverExample.Select("Select * from XYZ");
bs.DataSource = ds;
dataGridView.DataSoure = bs;
dataGridView.DataMember = "TableResult";
txtAbc.Bindings.Add("text",bs,"TableResult.Col1");
}
it worked,
but I have Button Add (which adds a new record to the database)
public void btnAdd_click()
{
DataDriverExample.Insert("Insert into XYZ values (\"Test\",\"Hello\",\"Bla\")");
DataSource ds = DataDriverExample.Select("Select * from XYZ");
bs.DataSource = ds;
bs.ResetBinding(true);
}
In my database it now has new record, but I get the error Cannot bind to the property or column on the DataSource
Sorry for my english not good.
Anyone can help me.
Tks
To Clean up your code, create another method for your binding something like:
private void LoadData()
{
bs.ResetBinding(true);
txtAbc.DataBindings.Clear()
DataSource ds = DataDriverExample.Select("Select * from XYZ");
bs.DataSource = ds;
dataGridView.DataSoure = bs;
dataGridView.DataMember = "TableResult";
txtAbc.Bindings.Add("text",bs,"TableResult.Col1");
}
Then just call LoadData(); on your formload and add method
public BindingSource bs = new BindingSource()
public void form_load()
{
LoadData();
}
Add:
public void btnAdd_click()
{
DataDriverExample.Insert("Insert into XYZ values (\"Test\",\"Hello\",\"Bla\")");
LoadData();
}
Hope this helps
Regards

loading combobox with datasource

i want to fill a combobox with data from the database when the page load
I had written the code as below
private void QuotationForm_Load(object sender, EventArgs e)
{
MessageBox.Show("hghjgvhg");
comboboxload();
}
public void comboboxload()
{
OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
oleDbConnection1.Open();
OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select jobpk,jobecode from jobcodemastertable",oleDbConnection1);
OleDbDataReader reader = oleDbCommand1.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("jobpk", typeof(int));
dt.Columns.Add("jobcode", typeof(string));
dt.Load(reader);
cmbjobcode.ValueMember = "jobpk";
cmbjobcode.DisplayMember = "jobcode";
cmbjobcode.DataSource = dt;
oleDbConnection1.Close();
}
it doesnot deturns an error or exception but doesnot load the combobox with data values
try this
comboBox1.DataSource = ds.Tables[0];
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
You may need to bind datatable's view with combo box
cmbjobcode.DataSource = dt.DefaultView;
You're missing the DataBind method
dt.Load(reader);
cmbjobcode.ValueMember = "jobpk";
cmbjobcode.DisplayMember = "jobcode";
cmbjobcode.DataSource = dt;
//here
cmbjobcode.DataBind();
oleDbConnection1.Close();
You have to call DataBind method on your combo. Thats why its not populating.

Categories

Resources