decoder
decoder

Reputation: 926

dynamic data binding gridview

my datatable formed as follows

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

Now i want to add checked image in place of true and unchecked image in place of false.how to bind grid view dynamically such that in place of disable check box i want to insert two types of image?

Upvotes: 1

Views: 3080

Answers (1)

R.C
R.C

Reputation: 10565

Your DataTable part is fine and continue to add the True/False text as per the logic. Now you should handle the GridView part. So, define an event handler for the OnRowDataBound event of GridView.

In this event only, check for the Text property of the cells, and if True/False, clear the cells and add the required image.

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

And your event handler will have code as below:

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

Upvotes: 1

Related Questions