簡體   English   中英

動態數據綁定GridView

[英]dynamic data binding gridview

我的數據表形成如下

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();

現在我想添加選中的圖像來代替true和未選中的圖像來代替false.how動態綁定網格視圖,以便代替禁用復選框,我想插入兩種類型的圖像?

您的DataTable部分很好,並根據邏輯繼續添加True / False文本。 現在,您應該處理GridView部分。 因此,為GridView的OnRowDataBound事件定義一個事件處理程序。

僅在這種情況下,檢查單元格的Text屬性,如果為True / False,則清除單元格並添加所需的圖像。

<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);
                }
            }
      }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM