如何在GridView上使用不同的数据源列?

时间:2023-02-11 15:40:37

I am trying to dynamically create a GridView. One of the columns is the user who created the row.

我正在尝试动态创建GridView。其中一列是创建行的用户。

JobDebrief jd = new JobDebrief(JobID);
Job jb = new Job(JobID);
DataGrid db = JobClass.Job_Piece.BuildGrid();
db.Columns.Add(CreateBoundColumn(jd.DbriefedByUser, "User"));
PlaceHolder.Controls.Add(db);
db.DataSource = jb.Pieces;
db.DataBind();

I created the GridView in the BuildGrid function which is in the job_piece class.

我在job_piece类中的BuildGrid函数中创建了GridView。

    public static DataGrid BuildGrid()
    {
          DataGrid NewDg = new DataGrid();

          NewDg.DataKeyField = "ID";
          NewDg.AutoGenerateColumns = false;
          NewDg.CssClass = "tblResults";
          NewDg.HeaderStyle.CssClass = "tblResultsHeader";
          NewDg.AlternatingItemStyle.CssClass = "ResultsStyleAlt";
          NewDg.ItemStyle.CssClass = "ResultsStyle";

          NewDg.Columns.Add(Load.CreateBoundColumn("AdvisedQty", "Qty Advised"));
          NewDg.Columns.Add(Load.CreateBoundColumn("PieceTypeString", "Piece Type"));
          NewDg.Columns.Add(Load.CreateBoundColumn("ReceivedQty", "Rcvd Qty"));          

          NewDg.Width = Unit.Percentage(100.00);

          return NewDg;
    }

public static BoundColumn CreateBoundColumn(string DataField, string Header,string CssClass ="",bool Highlight = false)
    {
        BoundColumn column = new BoundColumn();
        column.DataField = DataField;
        column.HeaderText = Header;
        column.SortExpression = DataField;

        if (Highlight)
        {
            column.ItemStyle.CssClass = "ColumnHighlight";
        }

        if (!string.IsNullOrEmpty(CssClass))
        {
            column.ItemStyle.CssClass = CssClass;
        }
        return column;
    }

The 3 columns it is currently displaying all come from job_piece. Since user doesn't belong to this class I tried to create the column outside of this function.

它当前显示的3列都来自job_piece。由于用户不属于此类,因此我尝试在此函数之外创建列。

The column displays the header but the rows are blank. The username comes from the JobDebrief class. But since I am binding the GridView to the pieces, db.DataSource = jb.Pieces; its not finding the information. Is it possible to set the user column to a different DataSource?

该列显示标题但行为空白。用户名来自JobDebrief类。但是因为我将GridView绑定到碎片上,所以db.DataSource = jb.Pieces;它找不到信息。是否可以将用户列设置为其他DataSource?

4 个解决方案

#1


4  

The simplest answar will be create a new datatable and assign all values in it

最简单的答案是创建一个新的数据表并在其中分配所有值

  DataTable dt= jb.Pieces.CopyToDataTable();
  dt.Columns.Add("UserId")
  for(int i=0;i<dt.Rows.Count;i++)
  {
        dt.Rows[i]=//Your info
  }
  db.DataSource = dt;
  db.DataBind();

#2


0  

In case you have list ( enumerable ) you can use join Based scenario. If one to one then join or else groupjoin.

如果您有列表(可枚举),则可以使用基于联接的方案。如果一对一然后加入或者groupjoin。

This will generate a single data source list and will be easy to bind or Retrieving data from database server then also can use join on database server.

这将生成单个数据源列表,并且易于绑定或从数据库服务器检索数据,然后也可以使用数据库服务器上的连接。

Technically i don't think so we can bind multiple data source to simple grid ( Except tree view and parent child view). Grid display the data in row format, So to generate the single row required single object or entity in collection. If you provide two data source there must be relation between both. For Ex:- first has a 10 row and second has a 20 then how may rows grid should display?. So for all this needs to use relation and create single view. that can be display in grid.

从技术上讲,我不认为我们可以将多个数据源绑定到简单网格(树视图和父子视图除外)。网格以行格式显示数据,因此要在集合中生成单行所需的单个对象或实体。如果提供两个数据源,则两者之间必须存在关联。对于Ex: - 首先有10行,第二行有20,那么行网格应该如何显示?所以这需要使用关系并创建单个视图。可以在网格中显示。

#3


0  

I think your best method is to use Linq. Tie your objects together by a common field such as an ID field. Now that the data is linked together you could display the User column it will now be in your object.

我认为你最好的方法是使用Linq。通过公共字段(如ID字段)将对象绑定在一起。现在数据已链接在一起,您可以显示它现在将在您的对象中的用户列。

Create your grid.

创建你的网格。

  <asp:GridView ID="NewDg" runat="server"  width="100%" AllowPaging="True" AlternatingRowStyle-CssClass="ResultsStyleAlt" AutoGenerateColumns="False" CssClass="tblResults" DataKeyNames="ID" EmptyDataText="No records Found" HeaderStyle-CssClass="tblResultsHeader" pagesize="10" RowStyle-CssClass="ResultsStyle" ShowFooter="False">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="LookUpID" ItemStyle-HorizontalAlign="Left" Visible="false" />
                        <asp:BoundField DataField="AdvisedQty" HeaderStyle-Width="250px" HeaderText="Qty Advised" ReadOnly="true" SortExpression="AdvisedQty" />
                        <asp:BoundField DataField="PieceTypeString" HeaderStyle-Width="150px" HeaderText="Piece Type" ItemStyle-HorizontalAlign="Left" ReadOnly="true" SortExpression="PieceTypeString" />
                        <asp:BoundField DataField="ReceivedQty" HeaderStyle-Width="150px" HeaderText="Rcvd Qty" ReadOnly="true" SortExpression="ReceivedQty" />
                        <asp:BoundField DataField="User" HeaderStyle-Width="150px" HeaderText="User" ReadOnly="true" SortExpression="User" />
                    </Columns>
                </asp:GridView>

Linq code on page load or when you want to load your grid

Linq代码在页面加载时或您想要加载网格时

   var combinedResults = (from p in jb.Peices
           join o in jb.JobDebrief
              on p.ID equals o.ID
                  select new
                  {p.AdvisedQty,
                   p.PieceTypeString,
                   p.ReceivedQty
                   o.User});

NewDg.Datasource = combinedResults.ToList;
NewDg.Databind();

If you can't combine the objects for some reason another thing that you could consider is to use the RowDataBound Method of your grid. When it creates your row check the ID in your grid if it equals the ID the column that you want then set the column equal to the User.

如果由于某种原因无法组合对象,则可以考虑的另一件事是使用网格的RowDataBound方法。当它创建行时,如果它等于ID所需的列,则检查网格中的ID,然后将列设置为等于用户。

--jb.Peices.ID =  jb.JobDebrief.ID

   if (e.Row.DataItem.ID == ID)
    {
        e.Row.Cells(4)==jb.JobDebrief.User
    };

#4


0  

You can create a wrapper class to wrap other classes having data needs to be bound to the grid. And then bind the grid to this wrapper class.

您可以创建一个包装类来包装具有数据需要绑定到网格的其他类。然后将网格绑定到此包装类。

The wrapper class will have both of the other classes as child members and will expose the data members from both classes that you need to bind to the grid.

包装器类将其他两个类作为子成员,并将从需要绑定到网格的两个类中公开数据成员。

#1


4  

The simplest answar will be create a new datatable and assign all values in it

最简单的答案是创建一个新的数据表并在其中分配所有值

  DataTable dt= jb.Pieces.CopyToDataTable();
  dt.Columns.Add("UserId")
  for(int i=0;i<dt.Rows.Count;i++)
  {
        dt.Rows[i]=//Your info
  }
  db.DataSource = dt;
  db.DataBind();

#2


0  

In case you have list ( enumerable ) you can use join Based scenario. If one to one then join or else groupjoin.

如果您有列表(可枚举),则可以使用基于联接的方案。如果一对一然后加入或者groupjoin。

This will generate a single data source list and will be easy to bind or Retrieving data from database server then also can use join on database server.

这将生成单个数据源列表,并且易于绑定或从数据库服务器检索数据,然后也可以使用数据库服务器上的连接。

Technically i don't think so we can bind multiple data source to simple grid ( Except tree view and parent child view). Grid display the data in row format, So to generate the single row required single object or entity in collection. If you provide two data source there must be relation between both. For Ex:- first has a 10 row and second has a 20 then how may rows grid should display?. So for all this needs to use relation and create single view. that can be display in grid.

从技术上讲,我不认为我们可以将多个数据源绑定到简单网格(树视图和父子视图除外)。网格以行格式显示数据,因此要在集合中生成单行所需的单个对象或实体。如果提供两个数据源,则两者之间必须存在关联。对于Ex: - 首先有10行,第二行有20,那么行网格应该如何显示?所以这需要使用关系并创建单个视图。可以在网格中显示。

#3


0  

I think your best method is to use Linq. Tie your objects together by a common field such as an ID field. Now that the data is linked together you could display the User column it will now be in your object.

我认为你最好的方法是使用Linq。通过公共字段(如ID字段)将对象绑定在一起。现在数据已链接在一起,您可以显示它现在将在您的对象中的用户列。

Create your grid.

创建你的网格。

  <asp:GridView ID="NewDg" runat="server"  width="100%" AllowPaging="True" AlternatingRowStyle-CssClass="ResultsStyleAlt" AutoGenerateColumns="False" CssClass="tblResults" DataKeyNames="ID" EmptyDataText="No records Found" HeaderStyle-CssClass="tblResultsHeader" pagesize="10" RowStyle-CssClass="ResultsStyle" ShowFooter="False">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="LookUpID" ItemStyle-HorizontalAlign="Left" Visible="false" />
                        <asp:BoundField DataField="AdvisedQty" HeaderStyle-Width="250px" HeaderText="Qty Advised" ReadOnly="true" SortExpression="AdvisedQty" />
                        <asp:BoundField DataField="PieceTypeString" HeaderStyle-Width="150px" HeaderText="Piece Type" ItemStyle-HorizontalAlign="Left" ReadOnly="true" SortExpression="PieceTypeString" />
                        <asp:BoundField DataField="ReceivedQty" HeaderStyle-Width="150px" HeaderText="Rcvd Qty" ReadOnly="true" SortExpression="ReceivedQty" />
                        <asp:BoundField DataField="User" HeaderStyle-Width="150px" HeaderText="User" ReadOnly="true" SortExpression="User" />
                    </Columns>
                </asp:GridView>

Linq code on page load or when you want to load your grid

Linq代码在页面加载时或您想要加载网格时

   var combinedResults = (from p in jb.Peices
           join o in jb.JobDebrief
              on p.ID equals o.ID
                  select new
                  {p.AdvisedQty,
                   p.PieceTypeString,
                   p.ReceivedQty
                   o.User});

NewDg.Datasource = combinedResults.ToList;
NewDg.Databind();

If you can't combine the objects for some reason another thing that you could consider is to use the RowDataBound Method of your grid. When it creates your row check the ID in your grid if it equals the ID the column that you want then set the column equal to the User.

如果由于某种原因无法组合对象,则可以考虑的另一件事是使用网格的RowDataBound方法。当它创建行时,如果它等于ID所需的列,则检查网格中的ID,然后将列设置为等于用户。

--jb.Peices.ID =  jb.JobDebrief.ID

   if (e.Row.DataItem.ID == ID)
    {
        e.Row.Cells(4)==jb.JobDebrief.User
    };

#4


0  

You can create a wrapper class to wrap other classes having data needs to be bound to the grid. And then bind the grid to this wrapper class.

您可以创建一个包装类来包装具有数据需要绑定到网格的其他类。然后将网格绑定到此包装类。

The wrapper class will have both of the other classes as child members and will expose the data members from both classes that you need to bind to the grid.

包装器类将其他两个类作为子成员,并将从需要绑定到网格的两个类中公开数据成员。