【问题标题】:Adding additional columns to winforms datagrid with datasource使用数据源将其他列添加到 winforms 数据网格
【发布时间】:2013-06-25 04:20:43
【问题描述】:

好吧,我第一次觉得我不得不发布,有很多关于数据网格的信息,但没有什么能以我理解的方式解释我需要什么。

        dataBooks.DataSource = null;
        dataBooks.AutoGenerateColumns = true;
        dataBooks.DataSource = _Author.Books.ToList();

这将返回一个对象列表,但是我想添加另一列,我可以在其中调用 getType() 它将返回书籍格式。

当我将 autogeneratecolumns 更改为 false 时,我不知道如何绑定数据,所以我得到了一个空白列表。温柔点,这对你来说可能很明显,但我是新手。

我想调用一个 GetBookType() 方法,它会返回一个字符串。

public abstract partial class Book
{
    public Book()
    {
        this.Orders = new HashSet<Order>();
    }

    public string AuthorName { get; set; }
    public string Title { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }
    public int Year { get; set; }

    public virtual Author Author { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}

以及将返回类型字符串的部分类

公共抽象部分类 clsBook { 公共覆盖字符串 ToS​​tring() { 返回 this.Title + "\t" + this.Year + "\t" + this.Price + "\t" + this.Quantity + "\t" + this.GetType(); }

    public abstract void EditDetails();

    public abstract string GetBookType();

【问题讨论】:

    标签: c# winforms datagrid


    【解决方案1】:

    您不需要设置AutoGenerateColumns = false,只需将列手动添加到您的DataGridView即可。我想你想添加一个名为NewColumn 的列,该列应该绑定到数据源的数据成员NewColumData。代码如下:

    DataGridViewTextBoxColumn newColumn = new DataGridViewTextBoxColumn(){Name = "NewColumn", DataPropertyName="NewColumnData"};
    dataBooks.Columns.Add(newColumn);
    dataBooks.DataSource = _Author.Books.ToList();
    

    以下是自动生成列的工作方式: 底层 DataSource 中的所有 DataMember 将循环通过,如果 DataMember 已被 DataGridView 中的某个现有 Column 绑定,则此列是而是使用,并且没有创建任何新列。在上面的代码中,您现有的列是NewColumn,此列已经通过属性DataPropertyName 绑定到您的DataSourceNewColumnData,因此使用此列而不是创建新列。所有其他列都会自动正常创建。

    更新

    我不明白你为什么想要一个像GetBookType 这样的方法,如果你想在一个列中显示它,只需为其添加一些属性,而不是像这样:

    public abstract partial class Book
    {
      public Book()
      {
        this.Orders = new HashSet<Order>();
      }
      //other properties of yours go here
      //--------------------------------
      //---------------------------------
      public string BookInfo {
           get {
              return this.Title + "\t" + this.Year + "\t" + this.Price + "\t" + this.Quantity + "\t" + this.GetType();
           }
      }
    }
    //Then you can bind your list to your DataGridView like this
    DataGridViewTextBoxColumn newColumn = new DataGridViewTextBoxColumn(){Name = "NewColumn", DataPropertyName="BookInfo"};//Notice the BookInfo which is assigned as the DataPropertyName for your new column.
    dataBooks.Columns.Add(newColumn);
    dataBooks.DataSource = _Author.Books.ToList();
    

    希望这次是你想要的。

    【讨论】:

    • 感谢 King King,那么我将如何调用一个方法来(填充)列表中每个对象的该列?
    • @redstubble 我不太明白你的问题,我认为列表中的每个对象都显示在 DataGridView 的一行中,其 DataSource 是列表。
    • 所以我有一个来自数据源 (_Author.Books.ToList();) 的所有书籍对象的列表,但我想添加另一列,我可以调用方法“GetType()”。这将返回它是什么类型的书的字符串。我想将此信息放在您帮助我创建的新列中。
    • 你能告诉我你的Book的类定义吗,我猜那是Book,你列表中的项目类型。列表类型应该有一些属性来描述它是什么类型的书。
    • @redstubble 请查看我的更新并随时询问更多信息 :)
    【解决方案2】:

    当 AutoGenerateColumns 为 false 时,必须为每个属性添加 DataGridTextColumn,如下所示,其中 Name 和 BookType 是 Book 类的属性。

    <sdk:DataGrid Name="dataGrid" AutoGenerateColumns="False" SelectionMode="Single">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn 
                    Header="Book Name" 
                    Binding="{Binding Name, Mode=TwoWay}"/>
                <sdk:DataGridTextColumn 
                    Header="Book Type" 
                    Binding="{Binding BookType, Mode=TwoWay}"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
    

    【讨论】:

    • 如何在Winforms 中使用这种脚本?
    • 您不能在 WinForms 中使用 XAML 脚本,但您可以在 WinForms 中使用单独的 WPF 控件
    • 但是 OP 将他的问题标记为 Winforms 而不是 WPF 我不认为 OP 想要解决这个问题,而他可以直接在 Winforms 中解决它。
    • 我想在winforms中解决这个问题,如果可能的话不添加额外的层。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多