ASP.NET DevExpress Gridview更新操作

时间:2017-06-25 18:29:46

标签: c# asp.net gridview devexpress

net devexpress控件。首先,我尝试在gridview上实现此编辑命令。一切似乎都很好,但我得到的只有一个问题是,当我试图点击编辑链接弹出窗体出来时,我无法在框内输入。最后,当我点击“更新”按钮时出现错误,说“对象引用未设置为对象的实例”。请帮助我解决这个问题,我哪里弄错了。感谢您。

<%@ Register assembly="DevExpress.Web.v17.1, Version=17.1.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web" tagprefix="dx" %>


<asp:Content runat="server" ID="BodyContent1" ContentPlaceHolderID="ContentPlaceHolder1">

<dx:ASPxGridView ID="GridView1" runat="server" ClientInstanceName="GridView1"
        KeyFieldName="TestConfigId" AutoGenerateColumns="False" Width="850px"            
        OnRowUpdating="Gridview1_RowUpdating" >
<Columns>             
            <dx:GridViewDataTextColumn Caption="TestConfigId" FieldName="TestConfigId" Name="TestConfigId" Visible="true" VisibleIndex="0">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Main Test" FieldName="MainTest" Name="MainTest" Settings-AllowSort="False" VisibleIndex="1" >
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Sub Test" FieldName="SubTest" Name="SubTest" Settings-AllowSort="False" VisibleIndex="2" >
            </dx:GridViewDataTextColumn>

<dx:GridViewCommandColumn  ShowEditButton="true" VisibleIndex="3" Width="100" FixedStyle="Left">
</dx:GridViewCommandColumn>

        </Columns>

        <Settings HorizontalScrollBarMode="Visible"/>
        <SettingsBehavior AutoExpandAllGroups="true"/>
        <SettingsBehavior AllowFocusedRow="True"/>
        <SettingsResizing ColumnResizeMode="NextColumn"/>

<Styles>
            <FixedColumn BackColor="LightYellow"></FixedColumn>
        </Styles>

        <Settings ShowStatusBar="Visible" />
        <SettingsEditing Mode="PopupEditForm" />
    </dx:ASPxGridView>
    <dx:ASPxPopupControl ID="popupSample" runat="server" ShowCloseButton="true" ShowHeader="true" 
    PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter">
    </dx:ASPxPopupControl>
</asp:Content>

文件背后的代码

 protected void Gridview1_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
{
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "update_test_configuration";
    cmd.Parameters.Add("@TestConfigId", SqlDbType.Int).Value = Convert.ToInt32(e.NewValues["TestConfigId"]);       
    cmd.Parameters.Add("@MainTest", SqlDbType.NVarChar).Value = e.NewValues["MainTest"].ToString();
    cmd.Parameters.Add("@SubTest", SqlDbType.NVarChar).Value = e.NewValues["SubTest"].ToString();

cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
con.Dispose();
}

存储过程

CREATE PROCEDURE [dbo].[update_test_configuration]
@TestConfigId int = 0,
@MainTest nvarchar(100),
@SubTest nvarchar(100)

AS

BEGIN

  SET NOCOUNT ON;

  UPDATE    dbo.TestConfigMaster 

  SET       MainTest    = @MainTest,
            SubTest     =@SubTest

    WHERE   TestConfigId = @TestConfigId                
END

1 个答案:

答案 0 :(得分:0)

如果e.NewValues [&#34; MainTest&#34;]为null,则e.NewValues [&#34; MainTest&#34;]。ToString()将生成错误,因为您无法调用ToString()空引用。即,你不能说&#34; null.ToString()。&#34;

相同将应用其余字段,即TestConfigId和SubTest。

也许你可以尝试这种方式:

cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter { SqlDbType = System.Data.SqlDbType.NVarChar, ParameterName = "@MainTest", Value = $"{ e.NewValues["MainTest"] }" });