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?