I am trying to databind a DataTable to an accordion and I have found that If I retrieve the DataTable from a database using a table adapter it binds to the accordion perfectly however what I want to do is create a dummy table (for testing purposes if I don't have access to my database) the code to create the dummy table is below:
DataTable table2 = new DataTable("articletable");
table2.Columns.Add("articleID");
table2.Columns.Add("title");
table2.Columns.Add("content");
DataRow row = table2.NewRow();
row[0] = "1";
row[1] = "article name";
row[2] = "article contents go here";
table2.Rows.Add(row);
When I try to data bind that table however the accordion does not display. I can bind it to a gridview or detailsview but not the accordion.
After 4 hours of banging my head against the wall, I discovered that the DataSource field is VERY picky.
Here's my code:
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Branch");
dt.Columns.Add("Officer");
dt.Columns.Add("CustAcct");
dt.Columns.Add("Grade");
dt.Columns.Add("Rate");
dt.Columns.Add("OrigBal");
dt.Columns.Add("BookBal");
dt.Columns.Add("Available");
dt.Columns.Add("Effective");
dt.Columns.Add("Maturity");
dt.Columns.Add("Collateral");
dt.Columns.Add("LoanSource");
dt.Columns.Add("RBCCode");
dt.Rows.Add(new object[] { "James Bond, LLC", 120, "Garrison Neely", "123 3428749020", 35, "6.000", "$24,590", "$13,432",
"$12,659", "12/13/21", "1/30/27", 55, "ILS", "R"});
ds.Tables.Add(dt);
accReportData.DataSourceID = null;
accReportData.DataSource = ds.Tables[0].DefaultView;
accReportData.DataBind();
Turns out that the accordion only likes being bound to a dataset table's defaultview. I tried binding to just a DataTable (dt) and it failed. Even dt.DefaultView failed. Once I added it to a DataSet, it binds like a champ. Very annoying, with lost of wasted time. I know you've probably long-since forgotten this, but I wanted to make it available to future searchers. Accordion.DataSource must be bound to a DataSet.Table.DefaultView to work.
Make sure you specify a type for the columns in the table2.Columns.Add(...)
Also, as seen in the answer below:
https://stackoverflow.com/a/6108163/637903
You can bind the Accordion Control to a DataTableReader constructed from the original DataTable
accReportData.DataSource = new System.Data.DataTableReader(ds.Tables[0]);
accReportData.DataBind();
Related
I want to sort a DataTable within a DataSet. I have the following code:
DataTable dt = ds.Tables[0];
dt.TableName = "NEWNAME";
dt.DefaultView.ApplyDefaultSort = false;
dt.DefaultView.Sort = "COL1 desc";
dt = dt.DefaultView.ToTable();
dt.AcceptChanges(); // <-- Break Point Here
ds.AcceptChanges();
As I step through the code beyond the break point in Visual Studio, checking on dt in VS visualizer shows the sorted table, but then checking on ds in VS visualiser does not show the table data in sorted order, even though the name change is reflected. I have tried multiple ways of sorting the datatable available in a google search but the outcome remains the same.
What am I doing wrong?
DefaultView.Sort doesn't sort anything. It is just a string that will be used when you require the construction of a new table like you do in the line
dt = dt.DefaultView.ToTable();
after this point the dt reference (correctly sorted with the info taken from DefaultView.Sort) is no more pointing to the ds.Tables[0]. It is an entirely new DataTable.
The other way in which the DefaultView.Sort applies is when you loop through the DefaultView like in
foreach(DataViewRow dvr in ds.Tables[0].DefaultView)
{
// Here you get a row from the table sorted according to the property.
DataRow row = dvr.Row;
.....
}
I've two DataGridView one very simple and one more complex with different column types.
My idea was to store store the data to XML and also load from the files at restart.
For the simple one it's no problem (dgvAccounts is the DataGridView):
DataTable dtusers = new DataTable("Users");
DataColumn col1 = new DataColumn("ID");
DataColumn col2 = new DataColumn("Key");
dtUsers.Columns.Add(col1);
dtUsers.Columns.Add(col2);
if (File.Exists("user.xml"))
dtUsers.ReadXml("user.xml");
dgvAccounts.DataSource = dtUsers;
....
dtUsers.WriteXml("user.xml");
But on the other DataGridView the DataTable I like to get from the DataSource is ever null (dgvActions is the DataGridView):
DataGridViewTextBoxColumn dgvcolA5 = new DataGridViewTextBoxColumn();
DataGridViewComboBoxColumn dgvcolA6 = new DataGridViewComboBoxColumn();
dgvActions.Columns.Add(dgvcolA5);
dgvActions.Columns.Add(dgvcolA6);
DataTable dtActions = (DataTable)dgvActions.DataSource;
dtActions.WriteXML("actions.xml");
I'd also tried to create a DataTable in advance and add it as dgvActions.DataSource but the result is ever the same.
So please can somebody help me to create a DataTable for the DataGridView or suggest any other way to store and load the data?
Thanks
Andre
What about setting up the datatable first and then setting it as the datasource like you do in the first example(then you wont get the null datatable from the datasource):
DataTable dtActions = new DataTable();
//Set DataTable columns here
dtActions.WriteXML("actions.xml");
dgvActions.DataSource = dtActions;
I'm looking for a way how is it bound that data from DataSource can be accessed via Item property on ListControl Class.
Can anyone give me an example how properties from passed object to DataSource are bound to generic class ListItemCollection? How that translation can be done by a code?
The translation I'd like to see is from DataSet to ListItemCollection. Thanks in advance for any help.
// Setting up a dataset. This dataset has no data; in real life you'd get the
// data from somewhere else, such as a database, and wouldn't need to build it.
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID"));
dt.Columns.Add(new DataColumn("Description"));
ds.Tables.Add(dt);
// Creating a list box. You'd probably have this declared in your HTML and wouldn't need to
// create it.
ListBox listBox1 = new ListBox();
listBox1.DataSource = ds.Tables[0];
listBox1.DataValueField = "ID";
listBox1.DataTextField = "Description";
listBox1.DataBind();
If your question is about how the binding is done behind the scenes, that has a fairly complex answer.
I'm trying to set the dataGridView as a datasource of some other datagridview, but it is not working. Please help me.
this.dataGridView2.DataSource = this.dataGridView1;
No compiler error, but not working as well.
this.dataGridView2.DataSource = this.dataGridView1;
this will show no compile error because this.dataGridView2.DataSource is expecting a type of object and you are assigning an object to it, but no datasource assigned here , to do that
this.dataGridView2.DataSource = this.dataGridView1.DataSource
assign like this
to Solve the Problem of changing data in datagrid one while changing data on first one
Try this
DataTable dt = (DataTable)dataGridView1.DataSource;
dataGridView2.DataSource = dt.Copy();
#Nighil sounds close, I don't know if that worked for you but I would not recomend using dataGridView as a datasource, I suggest you should separate out the logic keeping data separate from user controls, also using a 'BindingSource' gives you one easy method for refreshing data;
Say we had a method called 'getTablefromDatasource()' which got the data to be displayed and returned a 'DataTable'
DataTable table = getTablefromDatasource();
BindingSource source = new BindingSource();
dataGridView1.DataSource = source;
dataGridView2.DataSource = source;
then you can refresh the data with:
source.ResetBindings(false);
How to add child rows in datagrid c#.net windows forms?
I'm not sure if this is what you're asking, but if you want to append rows you're easiest way is to append them to whatever DataSource you're using before you DataBind()
If this wasn't what you're after, please provide more detail.
Usually you bind the datagrid to a dataset. Could you please clarify what you are looking for so that we can get into more detail?
DataTable myDataTable = new DataTable();
DataGridView myGridView = new DataGridView();
myGridView.DataSource = myDataTable;
DataRow row = myDataTable.Rows.Add(1, 2, 3, 4, 5); //This adds the new row
If you are looking for a nested table, you'll have to go with a third-party control. The DataGridView doesn't support it.
Firstly sure your data GridView name and create DataTable
DataTable DT= new DataTable();
then also create DataRow
DataRow row = new DataRow();
and last call the add() function Like this:
DT.Rows.Add("ID","Name","Addr","number");
and don't miss to set source on DataGrideView for Showing data :
DataGrideView.DataSource = DT;