将Gridview日期列与RowEditing事件中的给定日期进行比较

问题描述:

我很困惑,但这看起来很简单.

我有11列的Gridview.第8列是添加记录的日期.此日期作为DateTime存储在SQL Server表中.

屏幕上有一个编辑按钮,可使用后面C#代码中的RowEditing事件将用户带到更新屏幕.

我想做的就是将此添加日期与2012年8月1日进行比较.在此日期之前添加的任何记录都将显示一个更新屏幕,在此日期之后添加的每个记录都将显示一个不同的更新屏幕.

RowEditing事件使用UrlParameterPasser将记录ID传递给更新程序.

我不知道要怎么做的一件事是在所选单元格(第8列)中显示数据.我收到一个错误消息,该字符串未被识别为有效的DateTime,并且不确定如何显示程序看到的内容.

我也不确定如果可以解决错误,这种情况是否会解决.

任何帮助将不胜感激.

这是我的一些代码:

ASPX

I am stumped but it seems so easy.

I have a Gridview of 11 columns. Column 8 is the date of when the record was added. This date is stored as a DateTime in an SQL Server table.

On the screen there is an edit button that takes the user to an update screen using a RowEditing event in the C# code behind.

What I want to do is compare this Add date to 08/01/2012. Any record added before this date brings up one update screen and every record added after this date brings up a different update screen.

The RowEditing event uses UrlParameterPasser to pass the record ID to the update program.

One thing I do not know how to do is to show the data in the cell (column 8) selected. I get an error that the string is not recognized as a valid DateTime and am not sure how to display what the program sees.

I am also not sure if the condition would work if I could get around the error.

Any help will be much appreciated.

Here is some of my code:

ASPX

<asp:BoundField DataField="WelfareNumber" HeaderText="Welfare#" SortExpression="WelfareNumber" ItemStyle-Wrap="false" />
<asp:BoundField DataField="CPLastName" HeaderText="CP Last Name" SortExpression="CPLastName" ItemStyle-Wrap="false" />
<asp:BoundField DataField="CPFirstName" HeaderText="First Name" SortExpression="CPFirstName" ItemStyle-Wrap="false" />
<asp:BoundField DataField="CPParticipantID" HeaderText="CP Par ID" SortExpression="CPParticipantID" ItemStyle-Wrap="false" />
<asp:BoundField DataField="NPLastName" HeaderText="NP Last Name" SortExpression="NPLastName" ItemStyle-Wrap="false" />
<asp:BoundField DataField="NPFirstName" HeaderText="First Name" SortExpression="NPFirstName" ItemStyle-Wrap="false" />
<asp:BoundField DataField="NPParticipantID" HeaderText="NP Par ID" SortExpression="NPParticipantID" ItemStyle-Wrap="false" />
<asp:BoundField DataField="Added" HeaderText="Added" SortExpression="Added" ItemStyle-Wrap="false" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False" ItemStyle-ForeColor="LightSlateGray"/>
<asp:BoundField DataField="AddedBy" HeaderText="Added By" SortExpression="AddedBy" ItemStyle-Wrap="false" ItemStyle-ForeColor="LightSlateGray"/>
<asp:BoundField DataField="Updated" HeaderText="Updated" SortExpression="Updated" ItemStyle-Wrap="false" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False" ItemStyle-ForeColor="LightSlateGray"/>
<asp:BoundField DataField="UpdatedBy" HeaderText="Updated By" SortExpression="UpdatedBy" ItemStyle-Wrap="false" ItemStyle-ForeColor="LightSlateGray" />



ASPX.CS



ASPX.CS

protected void gvCAD_RowEditing(object sender, GridViewEditEventArgs e)
   {
       // Pass textbox values to ReceiveQueryString.aspx
       string strWhereToGo;
       string strAdded = gvCADs.Rows[e.NewEditIndex].Cells[8].Text;
       DateTime strAdded1;
       strAdded1 = DateTime.Parse(strAdded);
       strAdded = strAdded1.ToString("MM/dd/yyyy");
       if (Convert.ToDateTime(strAdded) > Convert.ToDateTime("07/30/2012"))
       {
           strWhereToGo = "~/CallCenter/UpdateCAD_2012.aspx";
           //UrlParameterPasser urlWrapper = new UrlParameterPasser("~/CallCenter/UpdateCAD_2012.aspx");
       }
       else
       {
           strWhereToGo = "~/CallCenter/UpdateCAD.aspx";
           //UrlParameterPasser urlWrapper = new UrlParameterPasser("~/CallCenter/UpdateCAD.aspx");
       }
       //UrlParameterPasser urlWrapper = new UrlParameterPasser("~/CallCenter/UpdateCAD.aspx");
       UrlParameterPasser urlWrapper = new UrlParameterPasser(strWhereToGo);
       // Add some values
       string CAD = ((Label)gvCADs.Rows[e.NewEditIndex].FindControl("itCPCADID")).Text;
       urlWrapper["CPCADID"] = CAD;
       //urlWrapper["age"] = age.Text;

       // Redirect to the page, passing the parameters in the querystring
       urlWrapper.PassParameters();
   }

我认为是行编辑事件的问题,因为当您单击编辑"按钮时,会发生行编辑事件,但这是在Gridview控件进入编辑模式之前发生的. br/>
通常,我们将此事件用作预防措施.这样就可以取消编辑.

我们在RowEdit中编写的最常用的代码是设置EditIndex:
gvCAD.EditIndex = e.NewEditIndex;

并将EditCacel事件设置为:
I feel rowediting event is the problem, because when you click Edit button RowEdit event occurs but this occurs before Gridview control get into edit mode.

Normally we use this event as a precaution/prevention. So that you can cancel editing.

The very usual code that we write in RowEdit is to set EditIndex:
gvCAD.EditIndex = e.NewEditIndex;

And EditCacel event as:
protected void gvCAD_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    //Reset the edit index.
    gvCAD.EditIndex = -1;

}




我建议您使用 gvCAD_RowUpdating 事件并尝试.

谢谢,

Kuthuparakkal




I propose you go with gvCAD_RowUpdating event and try.

Thanks,

Kuthuparakkal


由于报告服务问题,我通常使用VS2008而不是VS2005. VS2008调试器运行良好,我能够查看是什么数据导致了我的问题.在我在"gvCADs.Rows [e.NewEditIndex] .Cells [8] .Text;"中的Cells [8]上方提供的代码中.只需将其更改为包含已添加日期的Cells [9].

至于一旦上面的代码工作后又被踢回登录屏幕,我只需要在web.config文件中包括新的〜/CallCenter/UpdateCAD_2012.aspx"程序即可.

我希望这对其他人有帮助.向所有响应者表示感谢.有时,只需采取一些简单的建议即可将思考过程付诸实践.

在获得建议和帮助时,我对这个网站感到失望.很棒的网站!
I used VS2008 instead of VS2005 which is what I normally use due to the Report Services issue. The VS2008 debugger works well and I was able to see what data was causing my problem. In the code I provided above the Cells[8] in "gvCADs.Rows[e.NewEditIndex].Cells[8].Text;" just needed to be changed to Cells[9] which contained the Added date.

As for being kicked back to the login screen once the code above worked, I just had to include the new "~/CallCenter/UpdateCAD_2012.aspx" program in the web.config file.

I hope this helps others. And thanx to all the responders. Sometimes it just takes some simple advise to kick the thought process into action.

I have yet to be disappointed with this site when it comes to getting advise and help. Great site!!