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();
}
Related
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);
}
}
I'm trying to transfer this code to a class to avoid messy code in a form / user control. I display the data stored procedure successfully in my datagridview when the code is in my form / user control but when I tried to a class it doesn't work anymore. This is the code I'm working with in my class:
My class:
public class DisplayCustomer
{
public void Display_Customer(DataTable dt, DataGridView dgv)
{
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString))
{
using (var cmd = new SqlCommand("usp_GetCustomer", con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
using (var sda = new SqlDataAdapter(cmd))
{
dt = new DataTable();
sda.Fill(dt);
// dt.Load(cmd.ExecuteReader());
dgv = new DataGridView { DataSource = dt };
}
con.Close();
}
}
}
}
My user control:
public partial class ManageCustomer : UserControl
{
public ManageCustomer()
{
InitializeComponent();
}
public DataTable dbdataset;
private void ManageCustomer_Load(object sender, EventArgs e)
{
DisplayCustomer dc = new DisplayCustomer();
dc.Display_Customer(dbdataset, CustomersList);
}
}
I think there are two problems,
Use existing Datagridview, instead of create new.
dgv = new DataGridView { DataSource = dt };
try to do :
dgv.DataSource = dt;
Also, if your Datagridview located at UserControl, pass it as
ref
Display_Customer(DataTable dt, ref DataGridView dgv)
So, when you call it
dc.Display_Customer(dbdataset, ref CustomersList);
Let me know if this does not help. I am still on the way to office... So, didn't test the code. :)
try it and update me
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;
}
I make two datagridviews via msdn a now I need to get datatable from "detailsDataGridView", but I can't convert it.
Error: System.Windows.Forms.BindingSource can't be casted to type System.Data.DataTable.
Any idea?
The code, I have tried.
DataTable d = detailsDataGridView.DataSource as DataTable;
/////////////
DataTable dt = new DataTable();
dt = (DataTable)detailsDataGridView.DataSource;
////////////
BindingSource bs = new BindingSource();
bs.DataSource = detailsDataGridView.DataSource;
DataTable d = (DataTable)(bs.DataSource);
////////////
DataTable data = GetDataTableFromDGV(dgvMyMembers);
private DataTable GetDataTableFromDGV(DataGridView dgv)
{
var dt = ((DataTable)dgv.DataSource).Copy();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (!column.Visible)
{
dt.Columns.Remove(column.Name);
}
}
return dt;
}
Try This Method, This is What you need:
The DataGridView name is detailsDataGridView, so you'll do this :
DataTable data = GetDataTableFromDGV(detailsDataGridView);
Then data is the datatable!
private DataTable GetDataTableFromDGV(DataGridView dgv)
{
var dt = ((DataTable)dgv.DataSource).Copy();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (!column.Visible)
{
dt.Columns.Remove(column.Name);
}
}
return dt;
}
You can see my answer to this Question Here
Good Luck!
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