I am trying to merge two datarow cells into one. Here is my initializing code:
DataTable dt = new DataTable();
DataColumn dc;
DataRow dr;
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Col1";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Col2";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Col3";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Col4";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Col5";
dt.Columns.Add(dc);
This is the code I have for each column in Excel:
dr = dt.NewRow();
dr["Col1"] = "";
dr["Col2"] = "Quarter 1";
dr["Col3"] = "";
dr["Col4"] = "";
dr["Col5"] = "";
dt.Rows.Add(dr);
And it will look like this:
But I want it to look like this:
Any thoughts or ideas on how I can do this? I found it to be rather hard to merge the cells.
Related
Suppose I have all of these in my code. These items are in the search section and the user may perform multiple searches and if the created objects are not deleted, there will be a problem.
string myXMLfile =….;
DataSet ds = new DataSet();
ds.ReadXml(myXMLfile);
DataTable dt = ds.Tables["Table1"];
DataTable tempdt = new DataTable();
DataColumn Column = new DataColumn();
Column.DataType = System.Type.GetType("System.String");
Column.ColumnName = "Radif";
tempdt.Columns.Add(Column);
Column = new DataColumn();
Column.DataType = System.Type.GetType("System.String");
Column.ColumnName = "RootName";
tempdt.Columns.Add(Column);
…………
ds.Clear();
No.
[TestMethod]
public void TestClear()
{
DataSet ds = new DataSet();
ds.Tables.Add(new DataTable());
ds.Tables[0].Columns.Add("Column", typeof(int));
ds.Tables[0].Rows.Add(1);
Assert.AreEqual(1, ds.Tables.Count);
Assert.AreEqual(1, ds.Tables[0].Columns.Count);
Assert.AreEqual(1, ds.Tables[0].Rows.Count);
ds.Clear();
Assert.AreEqual(0, ds.Tables[0].Rows.Count);
Assert.AreEqual(1, ds.Tables[0].Columns.Count);
Assert.AreEqual(1, ds.Tables.Count);
}
I am selecting data from SQL into datagrid, the code works fine, but it is deleting the previous data from my datagrid.
My code (C# WPF SQL datagrid):
private void enter(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter)
{
SqlConnection com = new SqlConnection("Server = localhost; database = PrimaSOFT ; integrated security = true");
SqlCommand cmd = new SqlCommand("produktit", com);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Barkodi", txtBarkodi.Text);
//Created a new DataTable
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataColumn dc = new DataColumn();//Made a new DataColumn to populate above DataTable
dc.DataType = System.Type.GetType("System.String");//Defined the DataType inside, this can be [[int]] if you want.
dc.ColumnName = "Barkodi";//Gave it a name (important for the custom expression - can only be one word so use underscores if you need multiple words)
DataColumn dc2 = new DataColumn();
dc2.DataType = System.Type.GetType("System.String");
dc2.ColumnName = "Emertimi";
DataColumn dc3 = new DataColumn();
dc3.DataType = System.Type.GetType("System.Decimal");
dc3.ColumnName = "Sasia";
DataColumn dc4 = new DataColumn();
dc4.DataType = System.Type.GetType("System.Decimal");
dc4.ColumnName = "Cmimi";
DataColumn dc5 = new DataColumn();
dc5.DataType = System.Type.GetType("System.String");
dc5.Caption = "sds";
dc5.ColumnName = "TVSH";
DataColumn dc6 = new DataColumn();
dc6.DataType = System.Type.GetType("System.String");
dc6.ColumnName = "Total";
dc6.Expression = "Cmimi * Sasia";//Multiplying the Price and Quantity DataColumns
dataTable.Columns.Add(dc);//Add them to the DataTable
dataTable.Columns.Add(dc2);
dataTable.Columns.Add(dc3);
dataTable.Columns.Add(dc4);
dataTable.Columns.Add(dc5);
dataTable.Columns.Add(dc6);
dtgartikujt.ItemsSource = dataTable.DefaultView;//Set the DataGrid ItemSource to this new generated DataTable
com.Open();//Open the SQL connection
SqlDataReader reader = cmd.ExecuteReader();//Create a SqlDataReader
while (reader.Read())//For each row that the SQL query returns do
{
DataRow dr = dataTable.NewRow();//Create new DataRow to populate the DataTable (which is currently binded to the DataGrid)
dr[0] = reader[0];//Fill DataTable column 0 current row (Product) with reader[0] (Product from sql)
dr[1] = reader[1];
dr[2] = reader[2];
dr[3] = reader[3];
dr[4] = reader[4];
dataTable.Rows.Add(dr);//Add the new created DataRow to the DataTable
}
}
}
Sum code
object sumObject;
sumObject = dataTable.Compute("Sum(Totali)",
string.Empty);
txttotali.Text = sumObject.ToString();
I am expecting a new data to be showed in datagrid without deleting previous data.
How should I approach this?
Edited: I also attached the sum code, to sum the datagrid column, and display to textbox
As I think I understand what you are doing : separate dataTable code from Sql(ADO.NET) code and run dataTable part once only. So for run CreateTable in form_load for example :
public void CreateTable()
{
DataColumn dc = new DataColumn();//Made a new DataColumn to populate above DataTable
dc.DataType = System.Type.GetType("System.String");//Defined the DataType inside, this can be [[int]] if you want.
dc.ColumnName = "Barkodi";//Gave it a name (important for the custom expression - can only be one word so use underscores if you need multiple words)
DataColumn dc2 = new DataColumn();
dc2.DataType = System.Type.GetType("System.String");
dc2.ColumnName = "Emertimi";
DataColumn dc3 = new DataColumn();
dc3.DataType = System.Type.GetType("System.Decimal");
dc3.ColumnName = "Sasia";
DataColumn dc4 = new DataColumn();
dc4.DataType = System.Type.GetType("System.Decimal");
dc4.ColumnName = "Cmimi";
DataColumn dc5 = new DataColumn();
dc5.DataType = System.Type.GetType("System.String");
dc5.Caption = "sds";
dc5.ColumnName = "TVSH";
DataColumn dc6 = new DataColumn();
dc6.DataType = System.Type.GetType("System.String");
dc6.ColumnName = "Total";
dc6.Expression = "Cmimi * Sasia";//Multiplying the Price and Quantity DataColumns
dataTable.Columns.Add(dc);//Add them to the DataTable
dataTable.Columns.Add(dc2);
dataTable.Columns.Add(dc3);
dataTable.Columns.Add(dc4);
dataTable.Columns.Add(dc5);
dataTable.Columns.Add(dc6);
dtgartikujt.ItemsSource = dataTable.DefaultView;//Set the DataGrid ItemSource to this new generated DataTable
}
And the rest of code :
long sum=0;
private void enter(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter)
{
SqlConnection com = new SqlConnection("Server = localhost; database = PrimaSOFT ; integrated security = true");
SqlCommand cmd = new SqlCommand("produktit", com);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Barkodi", txtBarkodi.Text);
//Created a new DataTable
SqlDataAdapter da = new SqlDataAdapter(cmd);
com.Open();//Open the SQL connection
SqlDataReader reader = cmd.ExecuteReader();//Create a SqlDataReader
while (reader.Read())//For each row that the SQL query returns do
{
DataRow dr = dataTable.NewRow();//Create new DataRow to populate the DataTable (which is currently binded to the DataGrid)
dr[0] = reader[0];//Fill DataTable column 0 current row (Product) with reader[0] (Product from sql)
dr[1] = reader[1];
dr[2] = reader[2];
dr[3] = reader[3];
dr[4] = reader[4];
// sum+= Convert.ToInt64(reader[4]); // use the total index instead of 4 . or use reader["total"];
dataTable.Rows.Add(dr);//Add the new created DataRow to the DataTable
// or maybe dtgartikujt.Rows.Add(dr);
}
// textbox1.Text=sum.Tostring();
}
}
I am working with an application where user can multiply quantity and price and the results appear in third column using column expression
The code i used is showed under this :
DataTable dt = new DataTable("dtList");
DataTable dataTable = new DataTable();//Created a new DataTable
DataColumn dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Decimal");
dc.ColumnName = "Price";
DataColumn dc1 = new DataColumn();
dc1.DataType = System.Type.GetType("System.Decimal");
dc1.ColumnName = "qty";
DataColumn dc2 = new DataColumn();
dc2.DataType = System.Type.GetType("System.Decimal");
dc2.ColumnName = "Total";
dc2.Expression = "Price * qty";
dataTable.Columns.Add(dc);//Add them to the DataTable
dataTable.Columns.Add(dc1);
dataTable.Columns.Add(dc2);
object sumObject;
sumObject = dt.Compute("Sum(Total)", "");
I used this code to sum column Total, and show the result in a Text Box, but it's showing an error like this: "Cannot find column [Total]".
My question is what can i change, to make this code work, also sum automatically every time when the user change qty.
Sorry for my English and mistakes at my code I am new to WPF
Giving a fast check to your code I can see one thing.You define a DataTable called dt and then you try to compute the column "dt.Total" but you have never defined a column in you dt.
Try changing your:
sumObject = dt.Compute("Sum(Total)", "");
to
sumObject = dataTable.Compute("Sum(Total)", "");
Edit: By the way, if you do this:
dataTable = new DataTable();//Created a new DataTable
DataColumn dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Decimal");
dc.ColumnName = "Price";
DataColumn dc1 = new DataColumn();
dc1.DataType = System.Type.GetType("System.Decimal");
dc1.ColumnName = "qty";
DataColumn dc2 = new DataColumn();
dc2.DataType = System.Type.GetType("System.Decimal");
dc2.ColumnName = "Total";
dc2.DefaultValue = 5;
dc2.Expression = "Price * qty";
dataTable.Columns.Add(dc);//Add them to the DataTable
dataTable.Columns.Add(dc1);
dataTable.Columns.Add(dc2);
Your third column already has the value "price * qty".
If you want to show the sum of all your dc Total column just use:
object sumObject = dataTable.Compute("Sum(Total)", "");
And then
yourTextbox.Text = sumObject.ToString();
I am working with a small application where the user can retrieve specific data from SQL populate the datagrid with the data. The user can retrieve data from SQL Database where he write a barcode in textbox then the data he searched for will appear.
Until now i used this code
try
{
if (e.Key == Key.Enter)
{
SqlConnection con = new SqlConnection("Server = localhost;Database = Bilanc; Integrated Security = true");
SqlCommand cmd = new SqlCommand("product", con); // Using a Store Procedure.
cmd.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable("dtList");
cmd.Parameters.AddWithValue("#Barcod", txtcode.Text);
DataTable dataTable = new DataTable();//Created a new DataTable
DataColumn dc = new DataColumn();//Made a new DataColumn to populate above DataTable
dc.DataType = System.Type.GetType("System.String");//Defined the DataType inside, this can be [[int]] if you want.
dc.ColumnName = "#Barcod";//Gave it a name (important for the custom expression - can only be one word so use underscores if you need multiple words)
DataColumn dc2 = new DataColumn();
dc2.DataType = System.Type.GetType("System.String");
dc2.ColumnName = "#Product";
DataColumn dc3 = new DataColumn();
dc3.DataType = System.Type.GetType("System.Decimal");
dc3.ColumnName = "#QTY";
DataColumn dc4 = new DataColumn();
dc4.DataType = System.Type.GetType("System.Decimal");
dc4.ColumnName = "#Price";
DataColumn dc5 = new DataColumn();
dc5.DataType = System.Type.GetType("System.String");
dc5.ColumnName = "#Tax";
DataColumn dc6 = new DataColumn();
dc6.DataType = System.Type.GetType("System.String");
dc6.ColumnName = "Total";
dc6.Expression = "#Price * #QTY";//Multiplying the Price and Quantity DataColumns
dataTable.Columns.Add(dc);//Add them to the DataTable
dataTable.Columns.Add(dc2);
dataTable.Columns.Add(dc3);
dataTable.Columns.Add(dc4);
dataTable.Columns.Add(dc5);
dataTable.Columns.Add(dc6);
dtg.ItemsSource = dataTable.DefaultView;//Set the DataGrid ItemSource to this new generated DataTable
con.Open();//Open the SQL connection
SqlDataReader reader = cmd.ExecuteReader();//Create a SqlDataReader
while (reader.Read())//For each row that the SQL query returns do
{
DataRow dr = dataTable.NewRow();//Create new DataRow to populate the DataTable (which is currently binded to the DataGrid)
dr[0] = reader[0];//Fill DataTable column 0 current row (Product) with reader[0] (Product from sql)
dr[1] = reader[1];
dr[2] = reader[2];
dr[3] = reader[3];
dr[4] = reader[4];
dataTable.Rows.Add(dr);//Add the new created DataRow to the DataTable
}
}
}
catch (Exception)
{
MessageBox.Show("The product you are searching doesn't exist ");
}
}
The code works fine, but when i got stuck is when i retrieve data from SQL the new data overwrite the previous data.
My question how i can keep the previous data
Thanks to everyone
I am building datatable dynamically and the format is like following,
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User1";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User2";
dt.Columns.Add(dc);
dr["Question"] = "2D";
dr["User1"] = "1";
dr["Question"] = "3D";
dr["User1"] = "4";
dr["Question"] = "2D";
dr["User2"] = "2";
dr["Question"] = "3D";
dr["User2"] = "5";
How can I arrange is it like following,
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User1";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User2";
dt.Columns.Add(dc);
dr["Question"] = "2D";
dr["User1"] = "1";
dr["User2"] = "2";
dr["Question"] = "3D";
dr["User1"] = "4";
dr["User2"] = "5";
You can make linq cross join and filter it to list (you can bind list to your msChart)
var result = (from dr1 in dt.Select()
join dr2 in dt.Select() on dr1["Question"].ToString() equals dr2["Question"].ToString()
select new { q = dr1["question"], u1 = dr1["User1"], u2 = dr2["User2"] }
).Where(row => row.u1.ToString().Length > 0 && row.u2.ToString().Length > 0).ToList();
You could use code similar to this;
DataRow dr = dt.NewRow();
dr["Question"] = "2D";
dr["User1"] = "1";
dr["User2"] = "2";
dr = dt.NewRow();
dr["Question"] = "3D";
dr["User1"] = "4";
dr["User2"] = "5";
Supposing that dr is a variable of type DataRow obtained calling the DataTable method NewRow, then you can add the values to the row ItemArray property in just one line.
Of course you need to be absolutely sure of your columns order and type
DataRow dr = dt.NewRow();
dr.ItemArray = new object[] {"Q1", "1", "2"};
dt.Rows.Add(dr);
dr = dt.NewRow();
dr.ItemArray = new object[] {"Q2", "3", "4"};
dt.Rows.Add(dr);