ResetBinding in C# - 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

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);
}
}

Why does not work binding DataSource with DataTable?

I have code, that fill Datagridview from DataTable:
public void dataGridClientsLoad()
{
DataTable dt = new DataTable();
dt = serviceSqlite.select(new Pacients());
dataGridView1.DataSource = dt;
//dataGridView2.Update();
//dataGridView2.Refresh();
label1.Text = "Все" + updateCountRows();
}
In dt object I have get filled rows and columns(see pic.1, pic.2).
But as result, I have empty DataGridView after calling dataGridClientsLoad(), why?
ServiceSqlite is:
class SqliteService : ArraySelect
{
public SQLiteDatabase driver;
public SqliteService() {
SqliteConnection(new SQLiteDatabase());
}
public void SqliteConnection(SQLiteDatabase driver)
{
this.driver = driver;
}
public DataTable select(ISqlite select)
{
return select.select(driver);
}
}
You need to use DataBind() after setting data source to grid view-
dataGridView1.DataBind();
Binds the data source to the GridView control. This method cannot be inherited.
Use the DataBind() method to bind data from a data source to the GridView control. This method resolves all data-binding expressions in the active template of the control.refer MSDN
public void dataGridClientsLoad()
{
DataTable dt = new DataTable();
dt = serviceSqlite.select(new Pacients());
dataGridView1.DataSource = dt;
dataGridView1.DataBind();//This require to bind data table.
label1.Text = "Все" + updateCountRows();
}

Refresh button duplicate the datagridview data

I have problem when i try to refresh data in datagridview. Am using MySQL database. On every click on Reload button my old data is duplicated in grid. I try to set datagridview.DataSource = null also try to Refresh datagrid and also try to Resert binding source but nothing is happening.
Check this:
public MainForm()
{
InitializeComponent();
this.connStr = Properties.Settings.Default.connStr;
}
// Load
private void Form1_Load(object sender, EventArgs e)
{
SelectData();
}
// Seslect Data
public void SelectData()
{
bs.DataSource = GetData("SELECT * FROM porudzbine");
dataGridView1.DataSource = bs;
}
// Get Data
private DataTable GetData(string query)
{
try
{
conn = new MySqlConnection(connStr);
conn.Open();
adapter = new MySqlDataAdapter(query, conn);
adapter.Fill(dt);
}
catch(MySqlException ex)
{
MessageBox.Show(ex.Message.ToString());
}
return dt;
}
// Reload
private void osveziListuPorudzbinaButton_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
dataGridView1.Refresh();
bs.ResetBindings(false);
bs.DataSource = GetData("SELECT * FROM porudzbine");
dataGridView1.DataSource = bs;
}
You have a field called dt somewhere in your form (not shown in the post), which you are refilling (hence adding the records to the previously loaded records) on each GetData call.
Remove the field and use something like this:
// Get Data
private DataTable GetData(string query)
{
var dt = new DataTable();
// ...
return dt;
}

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.

Binding GridViewComboBoxColumn to a datasource

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();
}
}

Categories

Resources