0

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

        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; }
}

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

public abstract partial class clsBook { public override string ToString() { return this.Title + "\t" + this.Year + "\t" + this.Price + "\t" + this.Quantity + "\t" + this.GetType(); }

    public abstract void EditDetails();

    public abstract string GetBookType();
4

2 回答 2

2

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

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

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

更新

我不明白你为什么想要一个方法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();

我希望这一次,这是你想要的。

于 2013-06-25T05:04:08.993 回答
0

当 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>
于 2013-06-25T04:40:40.377 回答