C#DataGrid更新功能

时间:2013-10-21 09:21:56

标签: c# asp.net datagrid

目前我在DataGrid的更新功能方面遇到了麻烦。它无法在DataGrid中捕获我的输入。 有没有办法通过使用DataGrid捕获我的输入?因为大多数使用GridView的互联网信息都不同。

编辑: 这是一个具有更新,编辑和删除功能的DataGrid。 例如, 我选择一行数据并按下编辑按钮,填写所选行的数据,然后按更新按钮更新选定的行信息。 在这种情况下,我无法在编辑后捕获选定的行信息。 所以,我无法更新行数据。

以下是HTML代码

    <asp:DataGrid ID="dgRecords" runat="server" Width="100%" DataKeyField="InsitePriceID"
                    AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="5"
                    OnPageIndexChanged="dgRecords_PageIndexChanged" OnSortCommand="dgRecords_SortCommand"
                      OnDeleteCommand="dgRecords_OnDelete" OnEditCommand="dgRecords_OnEdit" OnUpdateCommand="dgRecords_OnUpdate"
                       OnCancelCommand="dgRecords_OnCancel">
                    <AlternatingItemStyle CssClass="Data"></AlternatingItemStyle>
                    <ItemStyle CssClass="Data"></ItemStyle>
                    <HeaderStyle CssClass="ColHeader"></HeaderStyle>
                    <Columns>
                        <asp:BoundColumn Visible="False" DataField="InsitePriceID"></asp:BoundColumn>
                        <asp:BoundColumn DataField="ServicePartFrom" HeaderText="No. of Service Part (From)"
                            SortExpression="ServicePartFrom"></asp:BoundColumn>
                        <asp:BoundColumn DataField="ServicePartTo" HeaderText="No. of Service Part (To)"
                            SortExpression="ServicePartTo"></asp:BoundColumn>
                        <asp:BoundColumn DataField="BaseAmount" HeaderText="% from Base Amount" SortExpression="BaseAmount">
                        </asp:BoundColumn>
                        <asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel" EditText="Edit"
                            UpdateText="Update"></asp:EditCommandColumn>
                        <asp:ButtonColumn ButtonType="PushButton" CommandName="Delete" Text="Delete"></asp:ButtonColumn>
                    </Columns>
                    <PagerStyle Mode="NumericPages"></PagerStyle>
                </asp:DataGrid>
<td style="width: 1014px">
                <asp:Label ID="lbla" runat="server"></asp:Label>
                <asp:Label ID="lblb" runat="server"></asp:Label>
                <asp:Label ID="lblc" runat="server"></asp:Label>
            </td>

代码背后。

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        ucWScr.SetValue("Insite Price");
        pnlMsg.Visible = false;
        BindData("");
    }
}

private void BindData(string _sortExpression)
    {
        clsAdmin obj = new clsAdmin();
        int intNoRec = 0;

        DataTable tbl = obj.SearchInsitePrice();

    }

protected void dgRecords_OnUpdate(object source, DataGridCommandEventArgs e)
{
    if (e.CommandName == "Update")
    {
        string strInsitePriceID = dgRecords.DataKeys[e.Item.ItemIndex].ToString();

//Error start from here

        lbla.Text = e.Item.Cells[1].Text;
        lblb.Text = e.Item.Cells[2].Text;
        lblc.Text = e.Item.Cells[3].Text;

        int intServicePartFrm = Int32.Parse(lbla.Text);
        int intServicePartTo = Int32.Parse(lblb.Text);
        int fltPercentBaseAmt = Int32.Parse(lblc.Text);

//Error ends here

        string strUserLogin = CurrentUser.UserLogin.ToString();
        DateTime dteNow = DateTime.Now;

        if (strInsitePriceID != "")
        {
            EStatus status = webform.UpdateInsitePrice(strInsitePriceID, intServicePartFrm, intServicePartTo, fltPercentBaseAmt, strUserLogin, dteNow);
            if (status != EStatus.Success) throw new Exception("Update failed.");

1 个答案:

答案 0 :(得分:0)

我使用这种方法解决了这个问题。

protected void dgRecords_OnUpdate(object source, DataGridCommandEventArgs e)
{
    if (e.CommandName == "Update")
    {
        string strInsitePriceID = dgRecords.DataKeys[e.Item.ItemIndex].ToString();
        TextBox temp, temp2, temp3;
        temp = (TextBox)e.Item.Cells[1].Controls[0];
        temp2 = (TextBox)e.Item.Cells[2].Controls[0];
        temp3 = (TextBox)e.Item.Cells[3].Controls[0];

        int intServicePartFrm = Int32.Parse(temp.Text);
        int intServicePartTo = Int32.Parse(temp2.Text);
        int fltPercentBaseAmt = Int32.Parse(temp3.Text);