1

我的数据表形成如下

DataTable dtFinalData = new DataTable();
        //Adding columns for BR details
        dtFinalData.Columns.Add("SupId", typeof(string));
        dtFinalData.Columns.Add("SupName", typeof(string));

        DateTime dt = DateTime.Today;
        int num = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);

        //--> adding columns for date part (1-31)
        for (int i = 1; i < num + 1; i++)
        {
            dtFinalData.Columns.Add(i.ToString(), typeof(bool));
        }
        //<-- adding columns for date part (1-31)


        #endregion

        #region Fill dtFinalData 

        //--> Looping each BR from tblBrList
        foreach (DataRow BrRow in dtBrList.Rows)
        {
            DataRow dr = dtFinalData.NewRow();

            int SupId = Convert.ToInt32(BrRow[0]); //retrieve BR ID from dtBrList
            String supName = BrRow[1].ToString(); //retreive Supervisor name from dtBrList

            dr["SupId"] = SupId.ToString();
            dr["SupName"] = supName;

            for (int i = 1; i < num + 1; i++)
            {

                DateTime dtRunningDate = new DateTime(2013, 5, i);

                //select BR_SUP_CODE, 
                DataRow[] drAttendance = dtAttendance.Select("BR_SUP_CODE=" + SupId + " AND SMS_DATE=#" + dtRunningDate + "#", string.Empty);
                if (drAttendance.Length == 1)
                {
                    //CheckBox chkbx = new CheckBox();
                    //chkbx.Checked = true;
                    dr[i.ToString()] = true;
                }
                else
                {
                    //CheckBox chkbx = new CheckBox();
                    //chkbx.Checked = false;
                    dr[i.ToString()] = false;
                }
            }

            dtFinalData.Rows.Add(dr);
        }
        //<-- Looping each BR from tblBrList


        #endregion

        GridView1.DataSource = dtFinalData;
        GridView1.DataBind();

现在我想添加选中的图像代替真和未选中的图像代替假。如何动态绑定网格视图,以便代替禁用复选框,我想插入两种类型的图像?

4

1 回答 1

1

您的DataTable部分很好,并按照逻辑继续添加 True/False 文本。现在你应该处理这个GridView部分。因此,为 GridView的OnRowDataBound事件定义一个事件处理程序。

仅在这种情况下,检查Text单元格的属性,如果是/否,则清除单元格并添加所需的图像。

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound"  ... />

您的事件处理程序将具有如下代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
            Image chkImage = new Image();
            chkImage.ImageUrl="~/images/Checked.png";
            Image UnchkImage = new Image();
            UnchkImage.ImageUrl = "~/images/UnChecked.png";

    if (e.Row.RowType == DataControlRowType.DataRow)
       {
         // We will start from 3rd cell because the first 2 cells are 
         // for SupID & SupName, where we don't need to place images
          for (int cellIndex = 2; cellIndex < GridView1.Columns.Count; cells++)
            {
               // Add Checked Image when cell text is true
                if (e.Row.Cells[cellIndex].Text == "true")
                {
                    // clear the cell and add only the image
                    e.Row.Cells[cellIndex].Controls.Clear();
                    e.Row.Cells[cellIndex].Controls.Add(chkImage);
                }
                // Add Unchecked Image when cell text is false
                if (e.Row.Cells[cellIndex].Text == "false")
                {
                    // clear the cell and add only the image
                    e.Row.Cells[cellIndex].Controls.Clear();
                    e.Row.Cells[cellIndex].Controls.Add(unchkImage);
                }
            }
      }
 }
于 2013-09-09T11:12:50.013 回答