473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

setting BindingSource data types from an XML data source

I have successfully bound an XmlDocument to a DataGridView but all fields
seem to be strings. I want to retrofit appropriate datatypes on some of
the fields. Let me take this in 2 parts.

Part I:
I found the ValueType field, leading me to believe that I could say

dataGridView.Columns["mean"].ValueType = typeof(Double);
dataGridView.Columns["sum"].ValueType = typeof(Integer);
etc.

I have not tried this but I suspect it would then, for example, sort a
column numerically rather than lexically when I click on the column
heading. Is that true?

Part II:
I want to dynamically adjust the bindingSource.Filter property to display
different subsets of the data in the DataGridView. So, for example, one
could say

bindingSource.Filter = "sum 0"

This actually works now, but I just realized that it is mere coincidence,
as they are really treated as strings.
When I tried this, however:

bindingSource.Filter = "value 5 * mean"

I received an EvaluationException saying that "*" could not be applied to
an Integer and a String. Which is due, I assume, to the fact that I set
the ValueType on the dataGridView, rather than on the bindingSource. So
how could I get this to work? I am guessing it is not as simple as just
setting the type but I need to attach a schema for the XML in some way....?
Aug 24 '06 #1
4 16701
Hi Michael,

The ValueType property of DataGridViewColumn can not be used to convert all
the values in that column from string to numeric type. Based on my
research, it is used as formatting purpose, which is leveraged by
DataGridViewCell.ValueType.

Can you tell me what effect you want to achieve by converting the data type
of a column? I think the correct solution to translate the data type is
creating an additional DataColumn(yes, I assume you have read the
XmlDocument to DataTable, and then bind the DataTable to the DataGridView)
with type of Integer or Double, then you have to read all the values in the
cell one by one and parse them into Integer/Double and assign to the cells
in the new column.

Regarding bindingSource.Filter property, it internally leverages underlying
DataView.RowFilter to implement the filtering work. While the syntax of
DataView.RowFilter is the same as DataColumn.Expression property. So you
should consult the MSDN link below for the correct syntax:
http://msdn2.microsoft.com/en-us/lib...n.expression.a
spx

For the specific formatting string below, I am not sure what "value" and
"mean" mean in your code context. I assume they are both DataColumn names:
bindingSource.Filter = "value 5 * mean"

Then, since mean DataColumn is still of type string, so it can not multiply
the integer of 5. So the exception will generate. By converting all the
values in the DataColumn from string to integer value, I think the
expression will be OK.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 25 '06 #2
Sorry for the delay in replying, Jeffrey :-)
I am not quite sure how to apply what you have stated to my situation, so
let me add some additional information that may state my need better. Here
is the essential code I am using (thanks in part to another post on this
forum) to go from my own generated XML into a DataGridView:

XmlDocument myDoc = GenerateAnalysisXML();
DataSet ds = new DataSet();
MemoryStream ms = new MemoryStream();
myDoc.Save(ms);
ms.Seek(0, 0);
ds.ReadXml(ms, XmlReadMode.Auto);
bindingSource.DataSource = ds;
bindingSource.DataMember = "device";
myDataGridView.DataSource = bindingSource;

Within the XML I have generated are columns "value" and "mean". As I
stated, if I use something like this:

bindingSource.Filter = "value 0"

....then it dynamically filters the DataGridView as desired, whereas this:

bindingSource.Filter = "value 5 * mean"

....causes an EvaluationException. So clearly in principle, as you state,I
need value and mean to be numerical types (integer or double). How do I go
about doing that, based on the above code?
Aug 29 '06 #3
Hi Henin,

Thanks for your feedback.

Oh, this lies with how you save the memory xml data into the disk, that is,
how you implement GenerateAnalysisXML() method.

There are 2 types of data you should save to disk xml files: 1. Xml data,
2. Xml Schema data.

We normally save the Xml data into the disk without any problem, however,
without the Xml schema information serialized to disk, the deserilizer does
not know how to parse the data in the xml file, so it always treats it as
string type.

For example, the below code snippet only saves the original DataSet data
into the disk:

private void button1_Click(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=i;
dr["column2"]="item"+i.ToString();
dt.Rows.Add(dr);
}
DataSet ds=new DataSet();
ds.Tables.Add(dt);

MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
ds.WriteXml("F:\\test.xml");
}

When reading the xml file, DataSet.ReadXml method does not know how to
parse the integer data in the column1, so it will always parse it as a
string. So after reading it in DataSet, we will find the column1's datatype
is string:
private void button2_Click(object sender, System.EventArgs e)
{
DataSet ds=new DataSet();
ds.ReadXml("F:\\test.xml");
MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
}

To get the correct type, you should serialize the schema information and
read the schema before reading data, like this:
private void button1_Click(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=i;
dr["column2"]="item"+i.ToString();
dt.Rows.Add(dr);
}
DataSet ds=new DataSet();
ds.Tables.Add(dt);

MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
ds.WriteXml("F:\\test.xml");
ds.WriteXmlSchema("F:\\testSchema.xml");
}

private void button2_Click(object sender, System.EventArgs e)
{
DataSet ds=new DataSet();
ds.ReadXmlSchema("F:\\testSchema.xml");
ds.ReadXml("F:\\test.xml");
MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
}
As you will see that, the column1's datatype will be correctly parse as
integer now.

So the key point is that you have to correctly generate the schema xml file
for the xml data.

If you failed to generate the xml schema inforamtion but still want to get
the correct data type in the reading, your reading code must know of the
schema information advance, so that you can manually construct the
DataTable/DataColumn schema type information by code before reading data,
like this:
private void button1_Click(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=i;
dr["column2"]="item"+i.ToString();
dt.Rows.Add(dr);
}
DataSet ds=new DataSet();
ds.Tables.Add(dt);

MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
ds.WriteXml("F:\\test.xml");

}

private void button2_Click(object sender, System.EventArgs e)
{
DataSet ds=new DataSet();
//construct the schema types manually with code
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
ds.Tables.Add(dt);

ds.ReadXml("F:\\test.xml");
MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
}
Once you construct the columns datatype before ReadXml, the data in xml
file will be parsed correctly to integer type.

If you still have anything unclear, or need any help, please feel free to
tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 30 '06 #4
Wow--your explanatation and discussion of alternatives have provided me
with a wealth of great information both for my current project and future
work. Thank you!

Aug 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
bplacker
by: bplacker | last post by:
Ok, so I'm opening a dataset, and creating a new bindingsource based on a dataset. Once I set the bindingsource, I fill a bunch of text boxes, checkboxes, and combo boxes with data from the...
4
by: shibeta | last post by:
Hello, I have problem with DataGridView and BindingSource. I have created DataSet and added TableData to it with manualy created columns (without DataAdapter - I'm not using MSSQL). On the Form I...
6
by: BD | last post by:
Hi, I have a "Cancel" button on a form to cancel any changes in the current item with the code BindingSource1.CancelEdit but this don't restore the older's values that had been changed. The...
3
by: msnews.microsoft.com | last post by:
Greetings I have a a simple application with 1 form. On my form I use a BindingSource to bind a database table to a "table (gridDataView)" (one in the database, one on my form, 2 different...
7
by: Mike | last post by:
i have a small difficulties with BindingSource and dataGridView bind db has properly opened and bind doesn't works. Unfortunately I didn't find any good example how to connect MS Access with...
1
by: Daniel Jeffrey | last post by:
..Net 2.0 VStudio 2005 C# I know this is going to seem like a strange question, as even I am sure I have missed something - but I cant find it. I want a simple event on any of the objects...
1
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, I used the Data Sources window to drop a custom object onto the designer surface. It created a BindingSource and BindingNavigator as a result. When I open the Properties window...
5
by: jehugaleahsa | last post by:
Hello: I am sure this question comes up a lot. I need to disable the controls on my Windows forms so that when the BindingSource is empty some the controls bound to it will be disabled. This...
2
by: Eric B. | last post by:
I could use a little clarification on using a BindingSource with my DataTable. As I understand it, the BindingSource sits between my data source (a DataTable) and DataGridView and any changes made...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.