Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to bind a datagridview from a datable using stored procedure and I have a calendar column in my datagridview, I try this code by it a gives me always the last value in the calendar column:

C#
grdEmployees.Rows.Clear();
           objGet = new GetClass();
           DataTable dt = objGet.GetValues("GetVacationForStartWork", txtEmployeeCode.Text.Trim());
           for (Int32 i = 0; i < dt.Rows.Count; i++)
           {
 grdEmployees.Rows.Add(Convert.ToDateTime(dt.Rows[i]["StartDate"]).ToString("MM/dd/yyyy")
                   , Convert.ToDateTime(dt.Rows[i]["EndDate"]).ToString("MM/dd/yyyy"),
                   dt.Rows[i]["IDVacation"]

             );
            

           }
           foreach (DataGridViewRow row in grdEmployees.Rows)
               {
                   for (Int32 i = 0; i < dt.Rows.Count; i++)
                   {
                       try
                       {
row.Cells["WorkStart"].Value = Convert.ToDateTime(row.Cells["EndDate"].Value).AddDays(1);
row.Cells["ConfirmedWorkStarted"].Value = Convert.ToDateTime(dt.Rows[i]["ConfirmedWorkStarted"]);
                       }
                       catch (Exception ex)
                       {
                           MessageBox.Show(ex.Message);
                       }

                   }

           }


My stored procedure output:
StartDate       EndDate         IDVacation     ConfirmedWorkStarted

2015-06-06	2015-06-26	  19	        6/30/2015 12:00:00 AM
2015-07-20	2015-07-25	  18	        7/27/2015 12:00:00 AM


DataGridView output:
CSS
StartDate       EndDate         IDVacation     ConfirmedWorkStarted

2015-06-06     2015-06-26           19                 7/27/2015 
2015-07-20     2015-07-25           18                 7/27/2015 

Any solution please ?
Posted
Updated 30-Jul-15 3:30am
v4
Comments
stibee 30-Jul-15 9:28am    
C#2.0 C# C#3.5 C#5 ? Your question is not clear for me? Please mark better what is not working
Leila Toumi 30-Jul-15 9:29am    
Please see the column "ConfirmedWorkStarted" in the stored procedure output and in the datagridview output , they are not the same!

Im not shure but Replace
C#
row.Cells["ConfirmedWorkStarted"].Value = Convert.ToDateTime(dt.Rows[i]["ConfirmedWorkStarted"]);


with:
C#
row.Cells["ConfirmedWorkStarted"].Value = Convert.ToDateTime(row.Cells["ConfirmedWorkStarted"])
 
Share this answer
 
The issue is with iterating the loops. Which is not correct. I did a sample on this. Check below.

DataTable dt = new DataTable();
dt.Columns.Add("StartDate");
dt.Columns.Add("EndDate");
dt.Columns.Add("IDVacation");
dt.Columns.Add("ConfirmedWorkStarted");

DataRow dr = dt.NewRow();
dr["StartDate"]="2015-06-06";
dr["EndDate"]="2015-06-26";
dr["IDVacation"]="19";
dr["ConfirmedWorkStarted"]="6/30/2015 12:00:00 AM";
dt.Rows.Add(dr);

DataRow dr1 = dt.NewRow();
dr1["StartDate"]="2015-07-20";
dr1["EndDate"]="2015-07-25";
dr1["IDVacation"]="18";
dr1["ConfirmedWorkStarted"]="7/27/2015 12:00:00 AM";
dt.Rows.Add(dr1);

grdEmployees.DataSource = dt;
grdEmployees.DataBind();

foreach (GridViewRow row in grdEmployees.Rows)
{
row.Cells[3].Text = Convert.ToDateTime(row.Cells[3].Text).ToShortDateString();

}

You can remove hard codes "3" and put your own counter i in it. Hope this works.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900