如何在gridview中更新行?

时间:2015-06-05 02:47:51

标签: c# asp.net linq twitter-bootstrap gridview

我已经尝试了三天以上的时间,让我的gridview进入一个bootstrap模板来编辑并随后更新一个选定的行。我得到了编辑部分最终工作,但我似乎无法使更新完成工作,虽然我已经使用了很多小时复制/粘贴来自不同网站的各种代码,并试图用我自己的想法调整它。网上似乎没有任何关于这个特定问题或至少没有完整的问题。

这是aspx / bootstrap网站的一部分:

 <asp:GridView ID="GridViewFindPatient" OnRowEditing="GridViewFindPatient_RowEditing" OnRowUpdating="GridViewFindPatient_RowUpdating" CssClass="table-responsive" runat="server" AllowPaging="True">
    <Columns>
        <%--<asp:boundfield datafield="insurance" headertext="Edit here:" />--%>
        <asp:TemplateField Headertext="">
            <ItemTemplate>
                <asp:ImageButton ID="btneditrowpat" runat="server" ImageUrl="~/img/editicon.gif" CommandName="Edit"/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField Headertext="">
            <ItemTemplate>
                <asp:ImageButton ID="btnsaverowpat" runat="server" ImageUrl="~/img/saveicon.gif" CommandName="Update"/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <PagerSettings FirstPageText="First" LastPageText="Last" PageButtonCount="4" />
</asp:GridView>

这是背后的代码:

public void GridViewFindPatient_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        GridViewRow row = (GridViewRow)GridViewFindPatient.Rows[e.RowIndex];

        string id = GridViewFindPatient.DataKeys[e.RowIndex].Value.ToString();

        TextBox fName = (TextBox)row.FindControl("firstName");
        TextBox lName = (TextBox)row.FindControl("lastName");
        TextBox addr = (TextBox)row.FindControl("address");
        TextBox pCode = (TextBox)row.FindControl("postalCode");
        TextBox city = (TextBox)row.FindControl("city");
        TextBox tel = (TextBox)row.FindControl("telephone");
        TextBox email = (TextBox)row.FindControl("email");
        TextBox patSSN = (TextBox)row.FindControl("patientSSN");
        CheckBox ins = (CheckBox)row.FindControl("insurance");


        care4uDataContext db = new care4uDataContext();

        var patSSNquery = (from pat in db.Patients
                          where pat.patientId == Convert.ToInt32(id)
                          select pat).First();

        patSSNquery.firstName = fName.Text;
        patSSNquery.lastName = lName.Text;
        patSSNquery.address = addr.Text;
        patSSNquery.postalCode = Convert.ToInt32(pCode.Text);
        patSSNquery.city = city.Text;
        patSSNquery.telephone = tel.Text;
        patSSNquery.email = email.Text;
        patSSNquery.insurance = Convert.ToBoolean(ins.Text);



        try
        {
            db.SubmitChanges();
        }
        catch (Exception exc)
        {
            Console.WriteLine(exc);
        }

        GridViewFindPatient.EditIndex = -1;
        PopulatePatGridview();
    }

关于RowUpdating命令,我在这里做错了。我试过各种组合但没有成功。希望有人在这里能够阐明这一点。

PS:我在使用上述代码时得到的最新错误是“数据库外”,但如果有人想知道,这不是主要问题,只是最新试验结果&amp;我的错误。

  

使用上面的代码抛出异常:

     

发生了'System.ArgumentOutOfRangeException'类型的异常   mscorlib.dll但未在用户代码中处理

     

System.ArgumentOutOfRangeException未被用户代码
处理   HResult = -2146233086消息=索引超出范围。一定是   非负且小于集合参数的大小。   Parametername:index Source = mscorlib ParamName = index StackTrace:          在System.Collections.ArrayList.get_Item(Int32索引)          在System.Web.UI.WebControls.DataKeyArray.get_Item(Int32索引)          at care4u1guib.patientedit.GridViewFindPatient_RowUpdating(Object sender,   GridViewUpdateEventArgs e)i c:\ Users \ X \ Documents \ Visual Studio   2013 \ Projects \ care4u1guib \ care4u1guib \ patientedit.aspx.cs:linje 62          在System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs)   E)          在System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow行,Int32 rowIndex,Boolean causeValidation)          在System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e,Boolean causeValidation,String validationGroup)          在System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source,EventArgs e)          在System.Web.UI.Control.RaiseBubbleEvent(Object source,EventArgs args)          在System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source,EventArgs e)          在System.Web.UI.Control.RaiseBubbleEvent(Object source,EventArgs args)          在System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e)          在System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String   eventArgument)          在System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String   eventArgument)          在System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)          在System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)          在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)
  的InnerException:

0 个答案:

没有答案