从combobox中选择值时如何获取数据表中的整行 [英] How to get the entire row in datatable when selecting value from combobox
问题描述
从组合框获取值时,如何获取特定行的值。或者当我从组合框中选择值CustomerName时,如何获取特定节点的值,让我们说xml中的PSID节点。实际上,我想要的是当用户从Combobox中选择客户名时获取PSID / CustomerID的值。我想要这个PSID自动填充文本框以方便用户。我不知怎的,我可以获得所选名称的PSID / CustomerID我可以轻松获得所需的值
how can i get the value of specific row when getting value from combobox . or how can i get the value of specific node let say PSID node in xml when im selecting the value CustomerName from combo box.Actually all i want is to get the value of PSID/CustomerID when User Select Customername from the Combobox. I want this PSID to Auto Fill the textboxes for User convenience. i somehow i can get PSID/CustomerID for Selected Name I can Easily get the req Values
<customers>
<Customer ID="16164613265169">
<billingdetails>
<customername>SulabhEdit
<address>Delhi Rohini</address>
<psid>16164613265169
<state>Delhi
<shippingdetails>
<name>SulabhEdit
<address>Delhi Rohini</address>
<state>Delhi
< b>我尝试了什么:
我使用Selected_index chnage来设计这里的值
What I have tried:
Im using the Selected_index chnage to manuplate the values here
private void cmbCustomerName_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(filepath);// file path is where my Above XML stored
DataTable dt = new DataTable();
dt = ds.Tables[1];
string currentvalue = cmbCustomerName.SelectedItem.ToString();
DataRowView DRV = (DataRowView)cmbCustomerName.SelectedItem;
DataRow row = DRV.Row;
//DataRow row = dt.Rows.Find(currentvalue);
string uniqueval = row[3].ToString();
XmlDocument xdoc = new XmlDocument();
xdoc.Load(filepath);
string nodepath = "//Customer ";
XmlNodeList xlist = xdoc.SelectNodes(nodepath);
XmlNode x = new XmlNode();
x.SelectNodes(
foreach (XmlNode eachnode in xlist)
{
}
}
方法是incomelete,我首先尝试获取值,以便我可以写下其余部分..任何人都可以帮助我紧急求助:)
Method is incomelete as im first try to get the value so that i can write rest of it.. can anybody help me its urgentthanks for help:)
推荐答案
以下内容可以轻松访问数据...
数据类:
The following will make it easy to access the data...
Data classes:
[XmlRoot(ElementName="billingdetails")]
public class Billingdetails {
[XmlElement(ElementName="customername")]
public string Customername { get; set; }
[XmlElement(ElementName="address")]
public string Address { get; set; }
[XmlElement(ElementName="psid")]
public string Psid { get; set; }
[XmlElement(ElementName="state")]
public string State { get; set; }
}
[XmlRoot(ElementName="shippingdetails")]
public class Shippingdetails {
[XmlElement(ElementName="name")]
public string Name { get; set; }
[XmlElement(ElementName="address")]
public string Address { get; set; }
[XmlElement(ElementName="state")]
public string State { get; set; }
}
[XmlRoot(ElementName="Customer")]
public class Customer {
[XmlElement(ElementName="billingdetails")]
public Billingdetails Billingdetails { get; set; }
[XmlElement(ElementName="shippingdetails")]
public Shippingdetails Shippingdetails { get; set; }
[XmlAttribute(AttributeName="ID")]
public string ID { get; set; }
}
[XmlRoot(ElementName="customers")]
public class Customers {
[XmlElement(ElementName="Customer")]
public List<Customer> Customer { get; set; }
}
自定义 XmlSerializerFactoryNoThrow
:
Custom XmlSerializerFactoryNoThrow
:
// ref: http://stackoverflow.com/questions/1127431/xmlserializer-giving-filenotfoundexception-at-constructor/39642834#39642834
public static class XmlSerializerFactoryNoThrow
{
public static Dictionary<Type, XmlSerializer> cache = new Dictionary<Type, XmlSerializer>();
private static object SyncRootCache = new object();
public static XmlSerializer Create(Type type)
{
XmlSerializer serializer;
lock (SyncRootCache)
if (cache.TryGetValue(type, out serializer))
return serializer;
lock (type) //multiple variable of type of one type is same instance
{
//constructor XmlSerializer.FromTypes does not throw the first chance exception
serializer = XmlSerializer.FromTypes(new[] { type })[0];
//serializer = XmlSerializerFactoryNoThrow.Create(type);
}
lock (SyncRootCache) cache[type] = serializer;
return serializer;
}
}
Core XML Helper
Core XML Helper
public static class XmlConverter
{
public static string FromClass<T>(T data, XmlSerializerNamespaces ns = null)
{
string response = string.Empty;
MemoryStream ms = new MemoryStream();
try
{
ms = FromClassToStream(data, ns);
if (ms != null)
{
ms.Position = 0;
using (StreamReader sr = new StreamReader(ms))
response = sr.ReadToEnd();
}
}
finally
{
// don't want memory leaks...
ms.Flush();
ms.Dispose();
ms = null;
}
return response;
}
public static MemoryStream FromClassToStream<T>(T data, XmlSerializerNamespaces ns = null)
{
MemoryStream stream = default(MemoryStream);
if (data != null)
{
XmlWriterSettings settings = new XmlWriterSettings()
{
Encoding = Encoding.UTF8,
Indent = true,
ConformanceLevel = ConformanceLevel.Auto,
CheckCharacters = true,
OmitXmlDeclaration = false
};
//XmlSerializer ser = new XmlSerializer(typeof(T));
XmlSerializer serializer = XmlSerializerFactoryNoThrow.Create(typeof(T));
stream = new MemoryStream();
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
serializer.Serialize(writer, data, ns);
writer.Flush();
}
stream.Position = 0;
}
return stream;
}
public static T ToClass<T>(string data)
{
T response = default(T);
if (!string.IsNullOrEmpty(data))
{
XmlReaderSettings settings = new XmlReaderSettings() { IgnoreWhitespace = true };
//XmlSerializer ser = new XmlSerializer(typeof(T));
XmlSerializer serializer = XmlSerializerFactoryNoThrow.Create(typeof(T));
XmlReader reader = XmlReader.Create(new StringReader(data), settings);
response = (T) Convert.ChangeType(serializer.Deserialize(reader), typeof(T));
}
return response;
}
}
使用:
To use:
var rawXml = IO.File.ReadAllText(fileName);
var customers = XmlConverter.ToClass<Customers>(rawXml);
BindList(comboBox1, customers.Customer, "ID", "ID");
以及绑定和用户ComboBox选择方法:
And the binding and user ComboBox selection methods:
public void BindList<TModel>(ListControl ctrl, IList<TModel> dataSource, string displayMember, string valueMember)
{
isBusy = true;
ctrl.DisplayMember = displayMember;
ctrl.ValueMember = valueMember;
ctrl.DataSource = dataSource;
isBusy = false;
}
private bool isBusy;
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
if (isBusy) return;
// User has made a selection
var selected = comboBox1.SelectedItem;
Debugger.Break();
}
我解决了它谢谢大家:)谢谢你的帮助
i Solved it Thanks Guys :) thank you for helping
这篇关于从combobox中选择值时如何获取数据表中的整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!