避免添加空行新行

时间:2012-07-25 15:25:21

标签: c# asp.net database webforms

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

如果我需要对我的解释更加透彻,请询问。

任何帮助将不胜感激。

谢谢

2 个答案:

答案 0 :(得分: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();
                }
            }


        }
    }

答案 1 :(得分:-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();
        }

    }