TreeView created from sql database with Checkbox wpf - c#

I create my TreeView with this piece of code (a method which returns DataSet and it will be equal to TreeView's DataContext):
MySqlConnection con = new MySqlConnection();
con.ConnectionString = ConString;
con.Open();
MySqlDataAdapter femda = new MySqlDataAdapter("SELECT * FROM fem_table", con);
MySqlDataAdapter fileda = new MySqlDataAdapter("SELECT * FROM file_table", con);
DataSet ds = new DataSet();
femda.Fill(ds, "fem_table");
fileda.Fill(ds, "file_table");
DataRelation dr = new DataRelation("DataRelationship",
ds.Tables["fem_table"].Columns["fem_guid"],
ds.Tables["file_table"].Columns["fk_gfem_guid"],true);
dr.Nested = true;
ds.Relations.Add(dr);
return ds;
I use HierarchialDataTemplate to fill the TreeView. In HierarchialDataTemplate, I have CheckBox. I want to reach the data of selected rows. I made some research but they are done with a new class and data from database is added to List of this class. How can I reach the data of selected rows if I use this way to fill the TreeView?

Related

Updating database from dataset in WPF datagrid

I have read all similar threads and I think I replicate the samples but the code still does not work
bind the grid:
sql="select * from myTable";
ds = new System.Data.DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter(sql,dbConnection._connection);
adp.Fill(ds);
ds.AcceptChanges();
grdTable.ItemsSource = ds.Tables[0].DefaultView;
Save data (on double-click here for simplicity, can be on button click)
private void grdTable_DoubleClick(object sender, MouseButtonEventArgs e)
{
adp.Update(ds.Tables[0]);
}
The data is displayed fine but the update statement gives an error:
"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."
All examples that I saw did not require creating UpdateCommand.
You need to use an OleDbCommandBuilder that automatically generates the commands for you: https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommandbuilder(v=vs.110).aspx
sql = "select * from myTable";
ds = new System.Data.DataSet();
OleDbDataAdapter adp = new OleDbDataAdapter();
adp.SelectCommand = new OleDbCommand(sql, dbConnection._connection);
OleDbCommandBuilder cb = new OleDbCommandBuilder(adp);
adp.Fill(ds);
ds.AcceptChanges();
grdTable.ItemsSource = ds.Tables[0].DefaultView;
Please refer to the above MSDN link for more information.

ComboBox data extraction?

my method is:
{
conn.Open();
SqlCommand cmd = new SqlCommand
("select BusinessEntityID from Person.Person order by'BusinessEntityID' ASC", conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
comboBox1.Items.Add(dr["BusinessEntityID"]);
}
conn.Close();
}
but that's not what I want. I have a class. update, insert, and delete list that also includes methods. a draw method of the ComboBox here I write data in the form side of the DataSource of the ComboBox I can I take it? In my system, every time the form on the side, I am forced to open and close connections.
for example, I take like DataGridView data
class side
public DataTable list()
{
SqlDataAdapter da = new SqlDataAdapter
("select * from HumanResources.Employee", OpenConnection());
DataTable dt = new DataTable();
DataSet ds = new DataSet();
da.Fill(dt);
CloseConnection();
return dt;
}
form side
dataGridView1.DataSource = data.list();

Using Select query to display data in a data grid view

I am trying to display a table data of my db in a DataGridView.here is my code
private void button1_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("Data Source=SHIRWANIPC;" +
"Initial Catalog=TEST DATABASE;" + "Integrated Security=True");
myConnection.Open();
SqlCommand objcmd = new SqlCommand("SELECT * FROM Customer", myConnection);
SqlDataAdapter adp = new SqlDataAdapter(objcmd);
DataTable dt = new DataTable();
//adp.Fill(dt);
DataGridView.DataSource = adp.Fill(dt);
}
The code is not giving any error now but but it dosent display the data from my table in my grid ?
You are using class name instead of object (instance) name of DataGridView, check in the html what is ID/name of DataGridView, It is probably Win Forms and you do not have DataBind method there. DataBind method is defined for GridView for ASP.net. Find more about DataGridView here.
DataGridViewObject.DataSource = adp.Fill(dt);
this is the answer to my quetion , hope it helps others who are new to this
SqlConnection myConnection = new SqlConnection("Data Source=SHIRWANIPC;" + "Initial Catalog=TEST DATABASE;" + "Integrated Security=True");
myConnection.Open();
SqlCommand objcmd = new SqlCommand("SELECT * FROM Customer", myConnection);
//objcmd.ExecuteNonQuery();
SqlDataAdapter adp = new SqlDataAdapter(objcmd);
DataTable dt = new DataTable();
adp.Fill(dt);
//MessageBox.Show(dt.ToString());
dataGridView1.DataSource = dt;
Create an instance of DatagridView:
DataGridView dview = new DataGridView();
....
dview.DataSource = adp.Fill(dt);
dview.DataBind();
You are directly accessing DataGridView properties and tryingto add data to the class. You need to create and an instance of a DataGridView first. Then assign the datable to the object.
Than bind the data to the DataGridView, otherwise the data is not bound to the control and will not be displayed.
DataGridView view = new DataGridView();
dview.DataSource = adp.Fill(dt);
dview.DataBind();
You are not exectuting the query
myConnection.Open();
SqlCommand objcmd = new SqlCommand("SELECT * FROM Customer", myConnection);
DataTable dt = new DataTable(); using
(SqlDataReader sqlDataReader = objcmd.ExecuteReader())
{
dt.Load(sqlDataReader);
sqlDataReader.Close();
}
DataGridView dataGridView1 = new DataGridView();
dataGridView1.DataSource = dt;

Edit datagridview in runtime and update database

I searched some posts but still cant get it work.
I have a window application written in C#, Here I bind the datagridview to my database:
public static DataSet GetDataSet(string sql)
{
using (SqlConnection cn = new SqlConnection(constr))
{
SqlDataAdapter da = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
and I bind the datagridview in another class:
string sql = "select Amount, Price, Description from myTable";
ds = DbHelper.GetDataSet(sql);
dataGridView2.DataSource = ds.Tables[0];
I created a button for user to update the database after they edit the datagridview:
string sql = "select Amount, Price, Description from myTable";
DbHelper.UpdateDataSet(sql, ds);
MessageBox.Show("done");
public static void UpdateDataSet(string sql, DataSet ds)
{
using (SqlConnection cn = new SqlConnection(constr))
{
SqlDataAdapter da = new SqlDataAdapter(sql, cn);
da.Update(ds);
}
}
Here is the error, occurs at the line da.Update(ds);:
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
Thanks for help.
EDIT
I can get it work with this:
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);
da.UpdateCommand = commandBuilder.GetUpdateCommand();
But only when I show the key in the datagridview as well, otherwise:
Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.
How to overcome this if I dont want to show the key in datagridview?
SqlCommandBuilder needs the Primary Key to update the field. You can still load the key to the DataGridView, but hide the column.

Updating MS Access Database Through DataGridView

hi guys I'm trying to save data to database through datagridview with a button but I get the following error every time I run the application and here's my code:
DataTable Table = new DataTable();
BindingSource bindingSource1 = new BindingSource();
string connetionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Database/Database.accdb;";
string sql = "SELECT * FROM IAE;";
OleDbConnection connection = new OleDbConnection(connetionString);
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
OleDbCommandBuilder cb = new OleDbCommandBuilder(dataadapter);
DataSet ds = new DataSet();
connection.Open();
dataGridView1.DataSource = ds.Tables["IAE"];
dataadapter.Update((DataTable)bindingSource1.DataSource);
connection.Close();
the error : Value Cannot be null. Parameter name: dataTable
need your help and thanks
Reason of error is your bindingSource1.DataSource property is NULL. Your are not assigning any DATASOURCE to your bindingsource class

Categories