tags:

views:

7

answers:

0

Note: Code is taken from this tutorial http://msdn.microsoft.com/en-us/library/aa992036%28VS.80%29.aspx

I currently have an editable gridview with only some columns editable by the user. In the background I want to record the time/date they have saved the data to the grid. This column 'Time' is not bound to the grid. Is there anyway I can still include this update when rebinding the newly edited data?

I'm also having a problem saving the updated data of my column ActivityTotal. I've been debugging that and following the variable, and it holds the correct value, but just doesnt seem to update. I am providing the codes below for both my gridview and the cs behind and would be grateful if someone could take a look, I assume it's a sort of minor sql issue on my part.

using System;

using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data;

namespace ABCProject { public partial class _Default : System.Web.UI.Page {

    private bool tableCopied = false;
    private DataTable originalDataTable;
    int activityTotal;
    //int overallTotal;
    int currentID;
    string activityName;
    string currentSymm;
    string currentClar;
    string currentCel;
    string currentCent;
    string currentConn;
    string currentActivityTotal;

    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList_CostCentres.DataBind();
    }

    protected void DropDownList_CostCentres_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList_Managers.DataBind();
    }

    protected void DropDownList_Managers_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList_Employees.DataBind();
    }


    protected void DropDownList_Employees_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView_ABC.DataBind();
    }

    protected void GridView_ABC_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
            if (!tableCopied)
            {
                originalDataTable = ((DataRowView)e.Row.DataItem).Row.Table.Copy();
                ViewState["originalValuesDataTable"] = originalDataTable;
                tableCopied = true;
            }
    }

    protected void UpdateButton_Click(object sender, EventArgs e)
    {
        originalDataTable = (DataTable)ViewState["originalValuesDataTable"];

        foreach (GridViewRow r in GridView_ABC.Rows)
            if (IsRowModified(r)) { GridView_ABC.UpdateRow(r.RowIndex, false); }

        // Rebind the Grid to repopulate the original values table.
        tableCopied = false;
        GridView_ABC.DataBind();
    }

    protected bool IsRowModified(GridViewRow r)
    {

        currentID = Convert.ToInt32(GridView_ABC.DataKeys[0].Values[0]);
        activityName = Convert.ToString(GridView_ABC.DataKeys[1].Values[1]);

        currentSymm = ((TextBox)r.FindControl("SymmTextBox")).Text;
        currentClar = ((TextBox)r.FindControl("ClarTextBox")).Text;
        currentCel = ((TextBox)r.FindControl("CelTextBox")).Text;
        currentCent = ((TextBox)r.FindControl("CentTextBox")).Text;
        currentConn = ((TextBox)r.FindControl("ConnTextBox")).Text;
        currentActivityTotal = ((TextBox)r.FindControl("ActivityTextBox")).Text;

        sumOfValues();
        getDate();

        DataRow row =
            originalDataTable.Select(String.Format("EmployeeID = {0} AND ActivityName = '{1}'", currentID, activityName))[0];

        if (!currentSymm.Equals(row["Symm"].ToString())) { return true; }
        if (!currentClar.Equals(row["Clar"].ToString())) { return true; }
        if (!currentCel.Equals(row["Cel"].ToString())) { return true; }
        if (!currentCent.Equals(row["Cent"].ToString())) { return true; }
        if (!currentConn.Equals(row["Conn"].ToString())) { return true; }
        if (!currentActivityTotal.Equals(row["ActivityTotal"].ToString())) { return true; }

        return false;
    }

    protected void sumOfValues()
    {
        int curSym = Convert.ToInt32(currentSymm);
        int curClar = Convert.ToInt32(currentClar);
        int curCel = Convert.ToInt32(currentCel);
        int curCent = Convert.ToInt32(currentCent);
        int curConn = Convert.ToInt32(currentConn);

        activityTotal = (curSym+curClar+curCel+curCent+curConn);
        currentActivityTotal = Convert.ToString(activityTotal);
        //overallTotal += activityTotal;

    }

    protected void getDate()
    {
        DateTime date = DateTime.Now;
        string shortDate = date.ToShortDateString();
    }


}

}

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
         ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
         DeleteCommand="DELETE FROM [employeeData] WHERE [Time] = @Time" 
         InsertCommand="INSERT INTO [employeeData] ([EmployeeID], [ActivityName], [Time], [Symm], [Clar], [Cel], [Cent], [Conn], [ActivityTotal], [Total]) VALUES (@EmployeeID, @ActivityName, @Time, @Symm, @Clar, @Cel, @Cent, @Conn, @ActivityTotal, @Total)" 
         SelectCommand="SELECT [EmployeeID], [ActivityName], [Time], [Symm], [Clar], [Cel], [Cent], [Conn], [ActivityTotal], [Total] FROM [employeeData] WHERE ([EmployeeID] = @EmployeeID)" 
         UpdateCommand="UPDATE [employeeData] SET [Symm] = @Symm, [Clar] = @Clar, [Cel] = @Cel, [Cent] = @Cent, [Conn] = @Conn, [ActivityTotal] = @ActivityTotal WHERE [EmployeeID] = @EmployeeID AND [ActivityName] = @ActivityName">
         <SelectParameters>
             <asp:ControlParameter ControlID="DropDownList_Employees" Name="EmployeeID" 
                 PropertyName="SelectedValue" Type="Int32" />
         </SelectParameters>
             <DeleteParameters>
            <asp:Parameter Name="Time" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
        <asp:Parameter Name="EmployeeID" Type="Int32" />
        <asp:Parameter Name="ActivityName" Type="String" />
            <asp:Parameter Name="Symm" Type="String" />
            <asp:Parameter Name="Clar" Type="String" />
            <asp:Parameter Name="Cel" Type="String" />
            <asp:Parameter Name="Cent" Type="String" />
            <asp:Parameter Name="Conn" Type="String" />
            <asp:Parameter Name="ActivityTotal" Type="String" />
            <asp:Parameter Name="OverallTotal" Type="String" />
            <asp:Parameter Name="Time" Type="String" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="EmployeeID" Type="Int32" />
            <asp:Parameter Name="ActivityName" Type="String" />
            <asp:Parameter Name="Time" Type="String" />
            <asp:Parameter Name="Symm" Type="String" />
            <asp:Parameter Name="Clar" Type="String" />
            <asp:Parameter Name="Cel" Type="String" />
            <asp:Parameter Name="Cent" Type="String" />
            <asp:Parameter Name="Conn" Type="String" />
            <asp:Parameter Name="ActivityTotal" Type="String" />
            <asp:Parameter Name="OverallTotal" Type="String" />
        </InsertParameters>
     </asp:SqlDataSource>
      <br />
      <br />
      <asp:GridView ID="GridView_ABC" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="EmployeeID,ActivityName" DataSourceID="SqlDataSource1" 
        onrowdatabound="GridView_ABC_RowDataBound" Width="60px">
        <Columns>
            <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" ReadOnly="True"
                SortExpression="EmployeeID" />
            <asp:BoundField DataField="ActivityName" HeaderText="ActivityName" ReadOnly="True"
                SortExpression="ActivityName" />
            <asp:TemplateField HeaderText="Symmetrix" SortExpression="Symm">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Symm") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="SymmTextBox" runat="server" Text='<%# Bind("Symm") %>' Width="60"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Clar" SortExpression="Clar">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Clar") %>' Width="60"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="ClarTextBox" runat="server" Text='<%# Bind("Clar") %>' Width="60"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Cel" SortExpression="Cel">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Cel") %>' Width="60"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="CelTextBox" runat="server" Text='<%# Bind("Cel") %>' Width="60"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Cent" SortExpression="Cent">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Cent") %>' Width="60"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="CentTextBox" runat="server" Text='<%# Bind("Cent") %>' Width="60"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Conn" SortExpression="Conn">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Conn") %>' Width="60"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="ConnTextBox" runat="server" Text='<%# Bind("Conn") %>' Width="60"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ActivityTotal" SortExpression="ActivityTotal">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("ActivityTotal") %>' Width="60" ReadOnly="True" BackColor="White" BorderColor="White" BorderStyle="None"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="ActivityTextBox" runat="server" Text='<%# Bind("ActivityTotal") %>' Width="60" ReadOnly="True" BackColor="White" BorderColor="White" BorderStyle="None"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>