动态将数据绑定到特定列

时间:2018-09-05 03:56:47

标签: c# asp.net

我有一个网格视图和两个日期选择器。当选择第一个日期选择器时,它将自动从我从选择器中选择的日期开始网格标题。现在我想将数据绑定到第一列和最后一列。其他列保留为文本框。在我的代码中,它仅绑定到first。我需要绑定到最后一列。我在页面加载事件中的isPostback()中调用了pageload方法。这是我的代码。

     private void PageLoad()
            {
                try
                {
                    string Date = txtFromDate.Text;
                    DateTime dateTime = Convert.ToDateTime(Date.ToString());

                    int dayNo = (int)dateTime.DayOfWeek;
                    int rem = 0;
                    Session["txtDayNo"] = dayNo;

                    MultiView1.ActiveViewIndex = 0;
                    string[] date = new string[7];

                    BoundField bfield = new BoundField();
                    bfield.HeaderText = "";
                    bfield.DataField = "Types";

                    gvASMexpenses.Columns.Add(bfield);

                    for (int i = 0; i < 7; i++)
                    {
                        CultureInfo local = CultureInfo.CurrentCulture;
                        if (dayNo < 7)
                        {
                            date[i] = local.DateTimeFormat.DayNames[dayNo];
                            rem = dayNo - (int)Session["txtDayNo"];
                        }
                        if (dayNo >= 7)
                        {
                            dayNo = 0;

                            for (int j = 0; j < rem; j++)
                            {
                                date[i] = local.DateTimeFormat.DayNames[dayNo];
                            }
                        }

                        TemplateField tfield = new TemplateField();
                        tfield.HeaderText = date[i];
                        gvASMexpenses.Columns.Add(tfield);
                        dayNo++;
                    }

                    BoundField totField = new BoundField();
                    totField.HeaderText = "Total (Rs.)";
                    totField.DataField = "Total (Rs.)";
                    gvASMexpenses.Columns.Add(totField);

                    BoundField itemCode = new BoundField();
                    itemCode.HeaderText = "ItemCode";
                    itemCode.DataField = "ItemCode";
                    gvASMexpenses.Columns.Add(itemCode);

                    this.setGrid();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }   


     private void setGrid()
        {
            try
            {
                string[] date = new string[7];
                DataRow dr = null;
                DataTable dt = new DataTable();
                List<Exp_EmpExpenceDTO> oData =
                    oExptemsApplyBL.GetExpencesHeader(Convert.ToInt32(Session["CompanyID"].ToString()),
                        Convert.ToInt32(Session["EmployeeID"].ToString()), (int)ExpenseItemType.ASM);
                dt.Columns.AddRange(new DataColumn[11] { 
                        new DataColumn("Types", typeof(string)),
                        new DataColumn("Sunday", typeof(string)),
                        new DataColumn("Monday", typeof(string)),
                        new DataColumn("Tuesday", typeof(string)),
                        new DataColumn("Wednesday", typeof(string)),
                        new DataColumn("Thursday", typeof(string)),
                        new DataColumn("Friday", typeof(string)),
                        new DataColumn("Saturday", typeof(string)),
                        new DataColumn("Total (Rs.)",typeof(string)),
                        new DataColumn("ItemCode",typeof(string)),
                        new DataColumn(" ",typeof(string))});

                //dr = dt.NewRow();
                //dr["Types"] = "Planned";
                //dt.Rows.Add(dr);
                //dr = dt.NewRow();
                //dr["Types"] = "Worked";
                //dt.Rows.Add(dr);

                foreach (var item in oData)
                {
                    dr = dt.NewRow();
                    dt.Rows.Add(item.ShortName);
                }

                //foreach (var itemCode in oData)
                //{
                //    dr = dt.NewRow();
                //    dt.Rows.Add(itemCode.ShortName);
                //}

                dt.Rows.Add("TOTAL (RS.)");
                gvASMexpenses.DataSource = dt;
                gvASMexpenses.DataBind();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

这仅绑定到网格的第一列。我评论了那些话

0 个答案:

没有答案