【问题标题】:C# DataGrid update functionC# DataGrid 更新函数
【发布时间】:2013-10-21 09:21:56
【问题描述】:

目前我在更新 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.");

【问题讨论】:

  • 你能指定“无法捕获我的输入”吗?这是否意味着您在那里获得了默认值?然后我猜你在回发时对DataGrid 进行数据绑定。添加if(!IsPostBack)BindDataGrid();
  • 是的,我从数据库中获取默认值显示,但我无法编辑行数据。我的更新功能出了点问题。
  • 正如我在上一条评论中提到的,我假设您也在回发中进行数据绑定。向我们展示您的Page_Load
  • 嗨 Tim Schmelter,如何为更新的行绑定数据?它在 BoundColumn 字段而不是文本框字段中,所以我不知道如何对其进行数据绑定和更新。

标签: c# asp.net datagrid


【解决方案1】:

我就是用这个方法解决的。

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);

【讨论】:

    猜你喜欢
    • 2019-06-21
    • 2013-12-25
    • 2022-01-15
    • 1970-01-01
    • 2013-06-25
    • 2014-04-11
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多