0

我创建了一个网格视图应用程序,在我的模板之外我有一个添加新行按钮。当我添加一个新行时,它会被放置一个编辑和删除按钮。我想要做的是当我单击添加新行按钮时,我希望它在编辑模式下打开新行,因此不能添加带有空信息的空白行。所以基本上如果我添加一个新行并且不输入信息,它将不会被创建。

如果我需要更彻底地解释我的解释,请询问。

任何帮助将不胜感激。

谢谢

4

2 回答 2

0

我最终自己解决了问题。它现在完美无缺。我也更改了数据库,但我提供了我的代码。我相信有一种更简单的方法,但这是我能做的最好的:如果你们能以更简单的方式提供意见,我将不胜感激。

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "Insert") //- this is needed to explain that the INSERT command will only work when INSERT is clicked
        {
            gv.DataBind();

            DataTable d = dbcon.GetDataTable("SELECT * FROM CIS.CIS_TRANS ORDER BY ID DESC", "ProjectCISConnectionString");

            string transCode = "", fundCode = "", BSA_CD = "", DP_TYPE = "";

            if (d.Rows.Count > 0)
            {
                transCode = d.Rows[0]["TRANS_CD"].ToString();
                fundCode = d.Rows[0]["FUND_CD"].ToString();
                BSA_CD = d.Rows[0]["BSA_CD"].ToString();
                DP_TYPE = d.Rows[0]["DP_TYPE"].ToString();

                if (transCode.Trim().Length > 0)
                {
                    dbcon.Execute("INSERT INTO CIS.CIS_TRANS (TRANS_CD) VALUES('')", "ProjectCISConnectionString");

                    gv.DataBind();
                }
            }

            gv.EditIndex = gv.Rows.Count - 1;

        }
        else if (e.CommandName == "Cancel")
        {
            DataTable d = dbcon.GetDataTable("SELECT * FROM CIS.CIS_TRANS ORDER BY ID DESC", "ProjectCISConnectionString");

            string transCode = "";

            if (d.Rows.Count > 0)
            {
                transCode = d.Rows[0]["TRANS_CD"].ToString();

                if (transCode.Trim().Length == 0)
                {
                    dbcon.Execute(string.Format("DELETE CIS.CIS_TRANS WHERE ID = '{0}'", d.Rows[0]["ID"]), "ProjectCISConnectionString");

                    gv.DataBind();
                }
            }


        }
    }
于 2012-08-06T23:16:36.023 回答
-3

这很简单,一旦添加了行:

您需要设置新添加行的编辑索引:

gv.EditIndex = gv.Rows.Count-1;

为 OP 编辑

这是肮脏的代码,我只是向您展示我的意思并很快将其搅打。假设您的页面上有一个名为 GridView1 的网格视图:

namespace HelpSO3
{
    public partial class _Default : System.Web.UI.Page
    {
        List<string> t = new List<string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string s = "hi";
                t.Add(s);
                GridView1.DataSource = t;
                GridView1.DataBind();
                Session["MyList"] = t;
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            t = (List<string>)Session["MyList"];
            t.Add("Another String");
            GridView1.DataSource = t;
            GridView1.DataBind();
            GridView1.EditIndex = GridView1.Rows.Count - 1;
            GridView1.DataBind();
            Session["MyList"] = t;
        }
    }
}

因此 Button1_Click 事件添加了一个值为“Another String”的新行,然后我们绑定网格视图并将EditIndex值设置为最新行并重新绑定。就这么简单。

在您的情况下,您的代码将变为:

 protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert") //- this is needed to explain that the INSERT command will only work when INSERT is clicked
        {
            dbcon.Execute("INSERT INTO PROJ_ASP (TRANS_CD) VALUES('')", "ProjectASPConnectionString");

            gv.DataBind();
            gv.EditIndex = gv.Rows.Count-1;
            gv.DataBind();
        }

    }
于 2012-07-25T15:55:09.247 回答