更新GridView C#中的列行

时间:2016-11-28 05:07:52

标签: c# asp.net gridview

我遇到了一个无法找到解决方案的问题。 如下图所示,我有一个gridView来显示事件列表。我想要的是将“eventEndDate”列值与当前日期进行比较,以便更新最后一列的行“eventStatus”。 enter image description here

所以,我添加了一个while循环来读取数据库并获取“EventEndDate”的值如下:

while (reader.Read())
        {

            result.Add(reader.GetValue(4).ToString());
        }
        reader.Close();

然后,在加载方法中我也添加了这些代码:

string currentDate = DateTime.Now.ToString("MMMM/d/yyyy");
            //endDate = DateTime.Now.ToString("MMMM/d/yyyy");
            //TextBox1.Text = Session["CountryName"].ToString();
            country = Session["CountryName"].ToString();
            SqlDataSource1.SelectCommand = "SELECT * FROM EventsEnglish WHERE CountryName ='" + country + "'";
            SqlDataSource1.DataBind();

            foreach (var lastDay in result)
            {

                string lastDayDate = Convert.ToDateTime(lastDay).ToString("MMMM/d/yyyy");

                if (DateTime.ParseExact(lastDayDate, "MMMM/d/yyyy", CultureInfo.InvariantCulture) > DateTime.ParseExact(currentDate, "MMMM/d/yyyy", CultureInfo.InvariantCulture))
                {

                    SqlDataSource1.UpdateCommand = "UPDATE EventsEnglish SET EventStatus = Open";
                    SqlDataSource1.DataBind();

                }
                else
                {
                    SqlDataSource1.UpdateCommand = "UPDATE EventsEnglish SET EventStatus = Close";
                    SqlDataSource1.DataBind();
                }

问题是,我没有收到任何错误消息,但事件状态没有更新... 那么,我做错了什么?

2 个答案:

答案 0 :(得分:2)

请使用ExecuteNonQuery()更新您的表格。

答案 1 :(得分:1)

我正在为一个项目做同样的事情。我使用这种类型的c#网格连接到sql server并且更新工作正常,(你只需要更改boundfield值,更新的sql命令和更新的参数。

<body>
    <form id="form1" runat="server">
    <div style="height: 300px">
        <asp:Button CssClass="btn btn-primary" ID="btn_back_to_admin" runat="server" Text="Back to Admin" OnClick="btn_back_to_admin_Click" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" Height="260px" style="margin-bottom: 116px">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField ShowEditButton="True" />
               <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
               <asp:BoundField DataField="Nombre" HeaderText="Nombre" SortExpression="Nombre" />
               <asp:BoundField DataField="Precio" HeaderText="Precio" SortExpression="Precio" />
               <asp:BoundField DataField="Codigo" HeaderText="Codigo" SortExpression="Codigo" />
               <asp:BoundField DataField="Garantia" HeaderText="Garantia" SortExpression="Garantia" />
               <asp:BoundField DataField="Marca" HeaderText="Marca" SortExpression="Marca" />
               <asp:BoundField DataField="AspectosTecnicos" HeaderText="AspectosTecnicos" SortExpression="AspectosTecnicos" />
               <asp:BoundField DataField="Fotografia1" HeaderText="Fotografia" SortExpression="Fotografia1" />
               <asp:BoundField DataField="Descripcion" HeaderText="Descripcion" SortExpression="Descripcion" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ShoppingConnectionString %>" DeleteCommand="DELETE FROM [main_product] WHERE [P_ID] = @P_ID" InsertCommand="INSERT INTO [main_product] ([Product_name], [MRP], [Our_Prize], [Discount], [Brand], [Brand_image], [Type_of_product], [Imagepath1], [Imagepath2], [Imagepath3], [Imagepath4], [Detail], [stock], [new_arrival], [best_seller], [best_offer]) VALUES (@Product_name, @MRP, @Our_Prize, @Discount, @Brand, @Brand_image, @Type_of_product, @Imagepath1, @Imagepath2, @Imagepath3, @Imagepath4, @Detail, @stock, @new_arrival, @best_seller, @best_offer)" SelectCommand="SELECT * FROM [FO_Productos]" UpdateCommand=" Exec FOSP_ActualizarProducto @ID , @Nombre, @Descripcion , @Codigo , @Marca , @Precio , @Garantia , @AspectosTecnicos , @Fotografia1 , @Fotografia1 ">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Product_name" Type="String" />
                <asp:Parameter Name="MRP" Type="Int32" />
                <asp:Parameter Name="Our_Prize" Type="Int32" />
                <asp:Parameter Name="Discount" Type="Int32" />
                <asp:Parameter Name="Brand" Type="String" />
                <asp:Parameter Name="Brand_image" Type="String" />
                <asp:Parameter Name="Type_of_product" Type="String" />
                <asp:Parameter Name="Imagepath1" Type="String" />
                <asp:Parameter Name="Imagepath2" Type="String" />
                <asp:Parameter Name="Imagepath3" Type="String" />
                <asp:Parameter Name="Imagepath4" Type="String" />
                <asp:Parameter Name="Detail" Type="String" />
                <asp:Parameter Name="stock" Type="Int32" />
                <asp:Parameter Name="new_arrival" Type="Int32" />
                <asp:Parameter Name="best_seller" Type="Int32" />
                <asp:Parameter Name="best_offer" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Nombre" Type="String" />
                <asp:Parameter Name="Precio" Type="Int32" />
                <asp:Parameter Name="Codigo" Type="Int32" />
                <asp:Parameter Name="Garantia" Type="Int32" />
                <asp:Parameter Name="Marca" Type="String" />
                <asp:Parameter Name="AspectosTecnicos" Type="String" />
                <asp:Parameter Name="Fotografia1" Type="String" />
                <asp:Parameter Name="Descripcion" Type="String" />
                <asp:Parameter Name="Imagepath2" Type="String" />
                <asp:Parameter Name="Imagepath3" Type="String" />
                <asp:Parameter Name="Imagepath4" Type="String" />
                <asp:Parameter Name="Detail" Type="String" />
                <asp:Parameter Name="stock" Type="Int32" />
                <asp:Parameter Name="new_arrival" Type="Int32" />
                <asp:Parameter Name="best_seller" Type="Int32" />
                <asp:Parameter Name="best_offer" Type="Int32" />
                <asp:Parameter Name="P_ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:Button ID="Button1"  CssClass="btn btn-primary" runat="server" Text="Back To admin Console" OnClick="Button1_Click" />

    </div>
    </form>
</body>