我有一个可序列化的数据表。我想在datagridview中添加一个组合框并使用DataGridViewComboBoxColumn comboColumn = new DataGridViewComboBoxColumn();
,但后来我无法序列化新的绑定,因为我试图序列化datagridview而不是数据表,这是不可能的。所以我想试试是否有任何方法可以将combobox列添加到数据表而不是datagridview,以便我能够序列化。
希望我有意义。
答案 0 :(得分:2)
嗯,它没有意义:)。序列化用于将数据转换为表单(Xml或二进制等),可用于不同系统之间的数据流。组合框不是数据。您无法序列化Windows窗体控件。
如果您愿意在序列化的XML或二进制文件中显示项目集合(组合框数据),则需要在数据表中添加一个集合。最好的方法是创建一个包含表格所有项目的类。然后在您的类中创建一个属性,其中包含所有下拉项。然后序列化该类。
在我们的评论讨论后进行编辑
因此,您可以在数据后在DataTable中添加集合。虽然它看起来不是一个非常注重性能的解决方案..所以要么你可以优化它,要么可以转移到一个类。下面是我能够在数据表中注入Collection并且序列化非常好的代码。
using (var sqlConnection = new SqlConnection("Data Source={yourdbserver};Initial Catalog=StudentsEnrolment;Integrated Security=True"))
{
using (var command = new SqlCommand("select * from students"))
{
command.Connection = sqlConnection;
try
{
sqlConnection.Open();
var dataAdapter = new SqlDataAdapter(command);
DataTable table = new DataTable();
dataAdapter.Fill(0, 10, table);
table.TableName = "Students";
table.Columns.Add(new DataColumn("TestingCollection", typeof(List<string>)));
var testingstring = new List<string>() { "String 1", "string 2", "string 3" };
foreach(DataRow datarow in table.Rows)
{
datarow.SetField("TestingCollection", testingstring);
}
var xmlWriter = new StringWriter();
table.WriteXml(xmlWriter);
Console.WriteLine(xmlWriter);
}
finally
{
sqlConnection.Close();
}
}
}
这是序列化XML在此
之后的样子<DocumentElement>
<Students>
<StudendId>5</StudendId>
<First_x0020_Name>VSK</First_x0020_Name>
<Last_x0020_Name>sas</Last_x0020_Name>
<Middle_x0020_Name>sas</Middle_x0020_Name>
<Email>v@test.com</Email>
<Cellphone>+255345345334</Cellphone>
<TelePhone>3454354334</TelePhone>
<CourseId>1001</CourseId>
<TestingCollection xmlns:xsd="http://www.w3.org/2001/XMLSchema\" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance\">
<string>String 1</string>
<string>string 2</string>
<string>string 3</string>
</TestingCollection>
</Students>
<Students>
<StudendId>6</StudendId>
<First_x0020_Name>Agrerer</First_x0020_Name>
<Last_x0020_Name>sas</Last_x0020_Name>
<Middle_x0020_Name>sas</Middle_x0020_Name>
<Email>v@test.com</Email>
<Cellphone>+255345345334</Cellphone>
<TelePhone>3454354334</TelePhone>
<TestingCollection xmlns:xsd="http://www.w3.org/2001/XMLSchema\" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance\">
<string>String 1</string>
<string>string 2</string>
<string>string 3</string>
</TestingCollection>
</Students>
<Students>
<StudendId>7</StudendId>
<First_x0020_Name>Scott</First_x0020_Name>
<Last_x0020_Name>sas</Last_x0020_Name>
<Middle_x0020_Name>sas</Middle_x0020_Name>
<Email>abc@test.com</Email>
<Cellphone>+255345345334</Cellphone>
<TelePhone>3454354334</TelePhone>
<TestingCollection xmlns:xsd="http://www.w3.org/2001/XMLSchema\" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance\">
<string>String 1</string>
<string>string 2</string>
<string>string 3</string>
</TestingCollection>
</Students>
<Students>
<StudendId>8</StudendId>
<First_x0020_Name>AMR/JJohnson</First_x0020_Name>
<Last_x0020_Name>asd</Last_x0020_Name>
<Middle_x0020_Name>asd</Middle_x0020_Name>
<Email>
asd
</Email>
<Cellphone>ads</Cellphone>
<TelePhone>asd</TelePhone>
<TestingCollection xmlns:xsd="http://www.w3.org/2001/XMLSchema\" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance\">
<string>String 1</string>
<string>string 2</string>
<string>string 3</string>
</TestingCollection>
</Students>
</DocumentElement>