10

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

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.

    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.

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?

2
  • Every row in new column should contain the same value jd.DbriefedByUser. Am i right ? Commented Jul 21, 2015 at 11:19
  • it seems there are not reasons to help you if you don't answer clarification questions . Commented Jul 21, 2015 at 19:12

4 Answers 4

4
+25

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();
0

In case you have list ( enumerable ) you can use join Based scenario. If one to one then join or else 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.

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.

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

   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.

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

   if (e.Row.DataItem.ID == ID)
    {
        e.Row.Cells(4)==jb.JobDebrief.User
    };
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.