按下“插入”按钮时编辑模式中的Gridview行? asp.net C#

时间:2016-04-15 06:30:03

标签: c# asp.net gridview

我在griview下面有一个gridview和一些控件。我从“control”插入值并按“Insert”按钮,然后将值插入gridview行。这是我在控件中插入值时的gridview图像 然后按“插入”按钮

image1

当我单击griview中的“编辑”按钮时,所选编辑行的值显示在“控件”下方,但是当我更新控件中的值并再次单击“插入”时,该行会自动编辑数据,但我不想要这个,我希望当我再次点击“插入”时,该行不应该在编辑模式下显示,这里是图像

image2

当我点击“插入”按钮时,我想要用新值替换行值。

这是我的 aspx.cs 代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", System.Type.GetType("System.Int32"));
        dt.Columns.Add("Column Name", System.Type.GetType("System.String"));
        dt.Columns.Add("Data Type", System.Type.GetType("System.String"));
        dt.Columns.Add("Allow Null", System.Type.GetType("System.Boolean"));
        dt.Columns.Add("Primary Key", System.Type.GetType("System.Boolean"));
        Session["MyDataTable"] = dt;
    }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
    DataTable t = (DataTable)Session["MyDataTable"];
    DataRow row1 = t.NewRow();
    row1["ID"] = t.Rows.Count + 1;
    row1["Column Name"] = TextBox1.Text;
    row1["Data Type"] = DropDownList1.Text;
    row1["Allow Null"] = Null.Checked == true ? "true" : "false";
    row1["Primary Key"] = Primary.Checked == true ? "true" : "false";
    t.Rows.Add(row1);
    Session["MyDataTable"] = t;
    GridView2.DataSource = t;
    GridView2.DataBind();
    TextBox1.Text = String.Empty;
    DropDownList1.Text = "Select DataType";
}

protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    DataTable t = (DataTable)Session["MyDataTable"];
    t.Rows.RemoveAt(e.RowIndex);
    GridView2.DataSource = t;
    GridView2.DataBind();
}

protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
    DataTable t = (DataTable)Session["MyDataTable"];
    //GridView2.EditIndex = e.NewEditIndex;
    // TextBox1.Enabled = true;
    //GridViewRow row = GridView2.Rows[e.NewEditIndex];
    TextBox1.Text =   GridView2.Rows[e.NewEditIndex].Cells[2].Text.ToString();
    DropDownList1.Text = GridView2.Rows[e.NewEditIndex].Cells[3].Text.ToString();
    GridView2.DataSource = t;
    GridView2.DataBind();
}

protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    DataTable t = (DataTable)Session["MyDataTable"];
    GridView2.EditIndex = -1;
    GridViewRow row = GridView2.Rows[e.RowIndex];
    GridView2.DataSource = t;
    GridView2.DataBind();
}

public void cancel(object sender, GridViewCancelEditEventArgs e)
{
    DataTable t = (DataTable)Session["MyDataTable"];
    GridView2.EditIndex = -1;
    GridViewRow row = GridView2.Rows[e.RowIndex];
    GridView2.DataSource = t;
    GridView2.DataBind();
}

0 个答案:

没有答案