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?
Using the below-mentioned code, you can add a column to the DataGridView manually. Here, I have created a function named as display () to add a column manually and call it at the form load event. Here, I am filling the dt3 data table with the employee names.
In this blog, I have tried my best, to create different data sources and bind them to the GridView since data source and Grid View are very important to work in any application. In the future, if I have found any other datasource, I will append it to this write-up. Hope it will be a good reference or starting point for beginners.
Or fill the data into two different tables, one that you show in the DataGridView and one you use for the dropdown list. Then the bind DataTable1 to the DataGridView and will show all the values in the data table. The second data table, dtSeasons in my example, you use for the combo box.
Or fill the data into two different tables, one that you show in the DataGridView and one you use for the dropdown list. Then the bind DataTable1 to the DataGridView and will show all the values in the data table.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With