Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to have columns on GridView with different data sources?

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?

like image 954
user123456789 Avatar asked Jul 17 '15 10:07

user123456789


People also ask

How to add a column to the datagridview manually?

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.

Is it possible to create different data sources for GridView?

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.

How to bind datatable1 to the datagridview?

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.

How to show all values in a dropdown list in datagridview?

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.


1 Answers

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();
like image 138
VISHMAY Avatar answered Oct 07 '22 11:10

VISHMAY