【问题标题】:How to add ComboBoxColumn to the data table and not to the Datagridview?如何将 ComboBoxColumn 添加到数据表而不是 Datagridview?
【发布时间】:2015-03-11 07:05:46
【问题描述】:

我有一个可序列化的数据表。我想向 datagridview 添加一个组合框列并使用 DataGridViewComboBoxColumn comboColumn = new DataGridViewComboBoxColumn(); 但我无法序列化新的绑定,因为我正在尝试序列化 datagridview 而不是数据表,这是不可能的。所以我想尝试是否有任何方法可以将组合框列添加到数据表而不是 datagridview 以便我能够序列化。

希望我说得通。

【问题讨论】:

    标签: c# winforms serialization datagridview datagridcomboboxcolumn


    【解决方案1】:

    嗯,这没有意义:)。序列化用于将数据转换为一种形式(Xml 或 Binary 等),可用于不同系统之间的数据流。组合框不是数据。不能序列化 Windows 窗体控件。

    如果您愿意在序列化的 XML 或二进制文件中显示项目集合(组合框的数据),则需要在数据表中添加一个集合。最好的方法是创建一个包含表中所有项目的类。然后在您的班级中创建一个包含所有下拉菜单项的属性。然后序列化那个类。

    我们在评论中讨论后编辑

    因此您可以在数据之后在 DataTable 中添加一个集合。虽然它看起来不是一个非常面向性能的解决方案..所以你可以优化它或者可以移动到一个类。下面是我能够在数据表中注入一个集合并且它的序列化非常好的代码。

    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>
    

    【讨论】:

    • 感谢您回复 Gaurav。你说的绝对有道理,但我的数据表是可变的。变量太大,很难在课堂上提及所有表格项目。在性能方面,我发现对数据表的对象进行序列化而不是对类进行序列化是很好的。
    • 您好 Jashwanth,在这种情况下,我将尝试在数据表中添加一个新列。尝试向该列中的列(可能是嵌套数据表)添加新集合。我会尝试编写一些代码来测试。
    • 什么不起作用?请告诉我错误。给我看看你的代码。
    猜你喜欢
    • 1970-01-01
    • 2011-04-24
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    相关资源
    最近更新 更多