Binding Datagridview to dataset - c#

I have a problem binding datagridview to dataset, when I debug code I can see that dataset is filled ok with data from database, but it wont show on datagridview...
Here is my code:
MySqlConnection conn = new MySqlConnection(connectionString);
string sql = #"select artikli.idArtikla, artikli.NazivArtikla
from artikli";
MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "Artikli");
// Bind the data table to the data grid
dataGridView1.DataSource = ds;
EDIT: Thx for answers, now when I can see my data in grid, what is the easiest way to allow inserting, deleting and editing in my grid and forwarding those cnages to my database table?

DataGridView doesn't know how to bind DataSet, you must bind a DataTable.
dataGridView1.DataSource = ds.Tables["Artikli"];
or
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Artikli";

You need to provide the which DataTable you want to bind to DataGridView.
Try:
dataGridView1.DataSource = ds.Tables[0];

You forgot to bind the dataset to gridview.
At the end of your code add the following line.. after datasource.
dataGridView1.DataBind();

Related

Single Datagridview Master-Details, no error but only shows "(Collection)"

I am using SQLite and I would like to use Master-Detail in a single DataGridView. No error return, I check the Master Table ("ASM") and the Details Table ("PART") their data are good. However, my gridAsmPart only shows one row with the word (Collection) on column PART. Any help will be appreciated.
public void LoadData()
{
var asm = new SQLiteDataAdapter("SELECT * FROM ASM", dB.connectionString);
var part = new SQLiteDataAdapter("SELECT * FROM PART", dB.connectionString);
var ds = new DataSet();
asm.Fill(ds, "ASM");
part.Fill(ds, "PART");
DataRelation data_relation = new DataRelation(
"ASM_PART",
ds.Tables["ASM"].Columns["PART"],
ds.Tables["PART"].Columns["PART"]);
ds.Relations.Add(data_relation);
gridAsmPart.DataSource = ds;
}
Edit 1: It seems that if I use the old DataGrid, it works.
Well… on the line of code… gridAsmPart.DataSource = ds; … ds is a DataSet and when you assign a DataSet to the grid as a DataSource, then you also need to specify “which” table in the DataSet the grid should display. That is what the grids DataMember is for.
Have you tried to set the grids DataMember property to something like…?
gridAsmPart.DataSource = ds;
gridAsmPart.DataMember = “ASM”;
Or if you want the “Part” table to display….
gridAsmPart.DataSource = ds;
gridAsmPart.DataMember = “PART”;

Update DataGridView to DataSet

I'm displaying information from a dataset in a datagridview. How do I update the dataset with the data in the datagridview after it's modified? (automaticly or through button) Using winforms,in visual studio 2010, c#
The first, you should create a new datatable to contains data from Datagridview
After that, in the buttun are:(i'm connecting with database is accsess(sql is the same))
DataTable dt = new DataTable();
dt = (DataTable)dataGridView1.DataSource;
OleDbDataAdapter adp = new OleDbDataAdapter("Your query", con);
OleDbCommandBuilder cmdb = new OleDbCommandBuilder(adp);
adp.Update(dt);
// con: the variable name to connecting with database

How to set Filter in run time ? for gridview in C#, Winforms, DevExpress

I attach new DataSource and DataSet on Run Time. I set the filter also in the Run Time but it shows error
Cannot find column [invoice_number]
my code :
// Create a data adapter.
OleDbDataAdapter adapter =
new OleDbDataAdapter("SELECT * FROM gridview", connection);
// Create and fill a dataset.
DataSet sourceDataSet = new DataSet();
adapter.Fill(sourceDataSet);
// Specify the data source for the grid control.
gridControl1.DataSource = sourceDataSet.Tables[0];
// error show in this line
invoiceBindingSource.Filter =
string.Format("invoice_number = '{0}'", textEdit5.Text);
but my OrionSystem Access Database has the Column "invoice_number" in the table gridview. What is my error ?
Or you can always set the GridView.ActiveFilterString property.
You're setting the filter on the bindingsource, but you set the datasource directly on the grid control.
You must set the datasource on the bindingsource, and then set the grid's datasource to the bindingsource:
// Create a data adapter.
OleDbDataAdapter adapter =
new OleDbDataAdapter("SELECT * FROM gridview", connection);
// Create and fill a dataset.
DataSet sourceDataSet = new DataSet();
adapter.Fill(sourceDataSet);
// Specify the data source for the bindingsource.
invoiceBindingSource.DataSource = sourceDataSet.Tables[0];
// Specify the data source for the grid control.
gridControl1.DataSource = invoiceBindingSource;
// error show in this line
invoiceBindingSource.Filter =
string.Format("invoice_number = '{0}'", textEdit5.Text);
Cheers

How to display a some records in CheckedListBox

I'm beginner in .net.
I need to display a some records in CheckedListBox.
I have table (Cat) in DataSet:
I need to display the content of the data table (color column) in CheckedListBox control.
How can it can be implemented?
It appears that CheckedListBox does not support binding, so this will not work as expected:
CheckedListBox1.DataSource = tempDataSet.Tables("Cat")
CheckedListBox1.DisplayMember = "Color"
CheckedListBox1.ValueMember = "ID"
You can use a Bindable CheckedListBox instead. Then you can bind at design time using the Properties window:
you can follow this link:
how to bind data in checkedlistbox in window application
or try this template:
SqlDataAdapter da = new SqlDataAdapter("SELECT NAME AC_CODE FROM AccountM where compcode='" + Compcls.Gcomp_cd + "'", con);
DataSet ds = new DataSet();
da.Fill(ds, "AccountM ");
checkedListBox1.DataSource = ds;
checkedListBox1.SelectedValue = "AC_CODE";
checkedListBox1.SelectedItem = "NAME";
Let's say if you save the checkedListBox1.SelectedValue in db you can do as follow:
Load you data into a data table, e.g. myDt
Loop each datarow in your datatable & set the checked status based on dr value
foreach (DataRow dr in myDt.Rows)
{
checkedListBox1.SelectedValue = dr[0].ToString();
checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, true);
}
Hope this helps...

Binding a DataGridView to a DataSet directly fails

A DataGridView bound by this code does not display information as expected:
dataGridView1.DataSource = ds;
here's the code for ds:
public DataSet ConnectandReadList()
{
DataSet ds = new DataSet();
string connection_string="Data Source=hermes;database=qcvalues; Integrated Security=SSPI;";
using (var myConnection = new SqlConnection(connection_string))
{
myConnection.Open();
var command = new SqlCommand(InitializeQuery(), myConnection);
var adapter = new SqlDataAdapter(command);
adapter.Fill(ds);
}
return ds;
}
Try binding to the table inside the dataset: dataGridView1.DataSource = ds.Tables[0];
From the documentation for the DataGridView.DataSource property, you can also bind to a DataSet and use the DataMember property:
When binding to a data source that contains multiple lists or tables, you must set the DataMember property to a string that specifies the list or table to bind to.
I think in this case the table name would be "Table" since you are not naming it explicitly.
Is ds a DataSet?
If so, try setting your DGV's DataMember to a DataTable within the DataSet or specifying your DataSet's DataTable for the DataSource.

Categories