1

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?

2
  • 1
    You want to display a row with only image?? Please show your GridView Markup too Commented Sep 9, 2013 at 8:35
  • 1
    want to dynamically create grid column Commented Sep 9, 2013 at 8:54

1 Answer 1

1

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);
                }
            }
      }
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.