I am trying to add data to a newly created column...
DataSet ds = new DataSet;
ds.Tables[0].Columns.Add("Count");
Data.DataSource = ds;
Data.DataBind();
Where do I add, a hardcoded count? (I want count column to have '4')
You add data to rows - so:
DataSet ds = new DataSet();
DataTable table = ds.Tables.Add();
table.Columns.Add("Count", typeof(int));
table.Rows.Add(4);
However, there are probably easier options; a DataSet seems massively overkill to bind a single value.
It technically is a repeater function but if I get the hardcoded function... I can make the code to make it flexible. I got it to work. I was adding a column to an existing table. This is what I did...
for int i=0; i<ds.tables[0].Rows.Count; i++)
{
ds.tables[0].Rows[i]["Count"] = "4";
}
Related
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”;
I searched the web and Stack Overflow and found lots of descriptions on how to fill a DataGridView with the content of a DataTable. But still it does not work for me. My DataGridView shows the correct number of columns and rows, but they appear empty.
I use following method:
public void ShowDataInGrid(ref DataTable table)
{
BindingSource sBind = new BindingSource();
dbView.Columns.Clear();
dbView.AutoGenerateColumns = false;
sBind.DataSource = table;
dbView.DataSource = sBind; //Add table to DataGridView
dbView.Columns.Add("Date", "Date");
}
Before this I created a DataGridView of name "dbView" via the designer. I am not even sure, whether I need sBind. Without it I can bind the table directly to dbView, with the same bad result.
I suspect my table is the problem. It origins from a database (SQLite) and has several columns and rows (one of the columns has the name "Date"). It is definately filled with readable data.
I mainly read the table in using following commands (after this I manipulate the data in several different steps, like changing strings and adding numbers...):
string sql = "select * from Bank";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
table.Load(reader);
reader.Close();
table.AcceptChanges();
I think the problem might be, that the table entries are stored as objects and not as string, and hence can't be shown. That's why I tried to force the content to be strings with the following change to my table:
DataTable dbTableClone = new DataTable();
dbTableClone.Load(reader);
SQLiteDataReader reader.Close();
dbTableClone.AcceptChanges();
string[] dBHeader = new string[dbTableClone.Columns.Count];
dBHeader = ReadHeaderFromDataTable(dbTableClone); //own funktion, which reads the header
DataTable table;
table.Clear();
//will first create dbTable as empty clone, so I can set DataTyp of each Column
table = dbTableClone.Clone();
for (int col = 0; col > dBHeader.Length; col++) //first set all columns as string
{
dbTable.Columns[col].DataType = typeof(string);
}
foreach (DataRow Row in dbTableClone.Rows)
{
dbTable.ImportRow(Row);
}
This did not help me neither.
Another idea: I found some comments on similar problems, where it got apparently solved with quote: "I designed columns in the VS datagridview designer. Not the column name, but the column DataPropertyName must match with fields in database." Unfortunately I don't seem to be able to do/understand this.
Following you see one row of my input table.
Try fetching and setting to GridView this way
SqlLiteConnection con = new SqlLiteConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=DB.mdf;Integrated Security=True");
con.Open();
SqlLiteDataAdapter adap = new SqlLiteDataAdapter("select * from Bank", con);
DataSet ds = new System.Data.DataSet();
adap.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
Comment everything you've done so far, try this and let me know if this works for you or not. Change connection according to your DB.
I solved the problem.
The DataTable was fine. The problem was the setup of my DataGridView dbView. I set up dbView in the designer and somehow gave it a datasource. Now I set the datasource to "none" (In "DataGridView Tasks") and my data appears as intended.
Thanks to M Adeel Khalid for looking at my stuff. Him assuring to me that my code for the link was right, made me find the solution eventually.
At the end I really only needed to use a single line:
dbView.DataSource = table;
I have below code, where in I am creating a dataset based on a query to Oracle mdb.
....
**OracleDataAdapter adapter = new OracleDataAdapter(sqlstr, conn);
OracleCommandBuilder builder = new OracleCommandBuilder(adapter);
DataSet dataset = new DataSet();
adapter.Fill(dataset);**
DataTable dataTable = dataset.Tables[0];
....
I would like to have the highlighted code in a loop, and then add the dataset objects to a common datatable outside the loop.
Do I need to use datatable.Merge()? The resultset in 'n' datasets need to be combined into a single DataTable, hence I am not sure Merge is the right way.
How can this be implemented?
Thanks
Presumably you are creating a new 'sqlstr' for each query to the Oracle mdb? If so you can create a list to put all your sqlstr into and then have a for loop to cycle through them like this:
List<string> sqlstrings = new List<string>();
//add your sqlstr's here
DataSet dataset = new DataSet();
for(int i = 0; i < sqlstrings.count; i++)
{
OracleDataAdapter adapter = new OracleDataAdapter(sqlstrings[i].ToString(), conn);
OracleCommandBuilder builder = new OracleCommandBuilder(adapter);
adapter.Fill(dataset.Tables[i]);
}
This will get all of your queries into 1 dataset with multiple tables. You can then create a data relation between all the tables as per here: http://msdn.microsoft.com/en-us/library/ay82azad(v=vs.110).aspx.
However, if you want to create just 1 DataTable then you can use 'Merge' as per the link supplied by Mithrandir. Of course, you would need to add this into the loop above and account for the first run (you can't merge table 1 with nothing). Create a new DataTable before the loop and do something like:
if(i > 0)
{
dtBigTable.Merge(dataset.Tables[i]);
}
else
{
//First table
dtBigTables = dataset.Tables[0]
}
Note: this is untested code. It's just to give you an idea and something to play with.
I want to know how to update a Datatable which is inside a dataset.I have a datatable in which i have details of some item.Now i want to add this into a dataset for some purpose and update it.Give me some suggesions to solve this..
this is my code:
DataRow dr;
dr = Basket_DataTable.NewRow();
Basket_DataTable.Rows.Add(dr);
dr["PId"] = getPId.ToString();
dr["ProductName"] = getProductName.ToString();
dr["ImagePath"] = getImagePath.ToString();
dr["ProductPrice"] = getProductPrice.ToString();
dr["Quantity"] = getQuantity.ToString();
dr["ProductDescription"] = getProductDescription.ToString();
dr["TotalPrice"] = getProductPrice.ToString();
Basket_DataTable.AcceptChanges();
Basket_DataTable is my datatable which i need to add to a dataset and update..
I believe you want to add new Rows to your existing DataTable in your DataSet. Instead of creating a new DataTable, your Basket_DataTable should refer to your data table in the data set.
Something like.
//Create new Row from your DataTable in DataSet
DataRow dr = yourDataSet.Tables["Basket_DataTable"].NewRow();
// here you can refer to your datatable with the index as well
//e.g. yourDataSet.Tables[0]
Basket_DataTable.Rows.Add(dr);
dr["PId"] = getPId.ToString();
dr["ProductName"] = getProductName.ToString();
dr["ImagePath"] = getImagePath.ToString();
dr["ProductPrice"] = getProductPrice.ToString();
dr["Quantity"] = getQuantity.ToString();
dr["ProductDescription"] = getProductDescription.ToString();
dr["TotalPrice"] = getProductPrice.ToString();
//Remember to add your row to the table.
yourDataSet.Tables["Basket_DataTable"].Rows.Add(dr);
In your current code you are not adding the new row to the datatable. Remember to include the row in the datatable.
If the DataTable is already in an existing DataSet, but you want to add it to another one, you need to create a copy first - A DataTable instance can only have one parent DataSet.
DataTable Basket_DataTable_copy = Basket_DataTable.Copy();
yourDataSet.Tables.Add(Basket_DataTable_copy);
Then you can do your updates on Basket_DataTable_copy
see DataTable.Copy on MSDN
I'm trying to do something very simple, yet I can't seem to find the proper way to do it.
I have a MySqlDataAdapter object which contains the result of a query. I want to parse the results of this query before rendering them in an ASP.NET Repeater. (convert date from Unix Epoch to human readable, that sort of things).
So far, I'm unable to simply find how to create a loop that will allow me to move row by row in my SQL result, parse a couple of fields and change the content of some fields.
I'm also uncertain if I can do this directly from the MySqlDataAdapter, or from a DataSet, or from a DataTable. Most of the examples I have seen on the web don't cover that specific topic.
First you can fill a DataTable with the results:
DataTable table = new DataTable();
adapter.Fill(table);
After that you can loop through the DataTable and do your modfications:
foreach (DataRow row in table.Rows) {
row["MyDate"] = DoMagic(row["MyDate"]);
}
You can not loop your result set over DataAdapter object. You should fill your results into a DataTable, then it will be very simple to loop the rows :
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet);
if (myDataSet.Tables.Count > 0)
{
foreach (DataRow drResult in myDataSet[0].Rows)
{
// Do some stuff here over your row...
}
}
Do it inside a dataset.
populate the dataset from the dataadapter, then loop through the records in the dataset and modify those before binding it to the repeater.
Something like:
DataSet ds = new DataSet();
da.Fill(ds);
foreach(DataRow row in ds.Tables[0].Rows)
{
row[0] = ParseThisField(row[0]);
}
// then do your databind
Is a bit of a hacky way to do it though, but should work well enough for your purposes.