开发者

asp.net gridview editindex

Please take a look at this:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument);

    if (e.CommandName == "Edit")
    {
        GridView1.EditIndex = index;
    }
}

This code-snippet puts an entire gridview-row into the edit mode.

But I need to put only the 3rd and 5th cell (suppose) of a row into edit mode.

How can I do so?

Some may suggest not using edit template. But I am only using code behind to manipulate my data in the gridview like the following:

aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridView___Test._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>

<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" Font-Names="Verdana" Font-Size="Small" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">            
        </asp:GridView>

    </div>
    </form>
</body>

</html>

code behind:

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CreateGridView();
            }
        }

        private void CreateGridView()
        {
            GridView1.Columns.Clear();

            DataTable dataTable = Book.GetBooksDataSet().Tables[0];

            CommandField cf = new CommandField();
            cf.ShowEditButton = true;

            GridView1.Columns.Add(cf);

            foreach (DataColumn c in dataTable.Columns)
            {
                BoundField boundField = new BoundField();

                boundField.DataField = c.ColumnName;
                boundField.HeaderText = c.ColumnName;

                GridView1.Columns.Add(boundField);
            }

            GridView1.DataSource = dataTable;
            GridView1.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName == "Edit")
            {
                GridView1.EditIndex = index;

                GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];

                string id = selectedRow.Cells[1].Text;
                string isbn = selectedRow.Cells[2].Text;

                CreateGridView();
            }
            else if (e.CommandName == "Update")
            {
                LinkButton updateButton = (LinkButton开发者_开发百科)e.CommandSource;

                DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;

                GridViewRow gvr = (GridViewRow)dcfc.Parent;

                ControlCollection cc = gvr.Cells[1].Controls;

                TextBox tb = (TextBox)cc[0];

                GridView1.EditIndex = -1;

                CreateGridView();
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {            
        }
    }

And my view is to avoid asp.net scripting as much as possible.

And also what is possible using scripts, must be possible by only code.


see if following helps. i.e. put non editable columns propery InsertVisible to false. Further you can put the following code in GridView1_DataBinding event. remove line GridView1.DataBind(); and just call again GridView1.DataBind(); where you call this method.

private void CreateGridView()
        {
            GridView GridView1 = new GridView();
            GridView1.Columns.Clear();

            DataTable dataTable = Book.GetBooksDataSet().Tables[0];

            CommandField cf = new CommandField();
            cf.ShowEditButton = true;

            GridView1.Columns.Add(cf);

            int colCount = 1;
            foreach (DataColumn c in dataTable.Columns)
            {
                BoundField boundField = new BoundField();

                boundField.DataField = c.ColumnName;
                boundField.HeaderText = c.ColumnName;
                if (colCount == 3 | colCount == 5)
                {
                    boundField.InsertVisible = true;
                }
                else
                {
                    boundField.InsertVisible = false;
                }

                colCount++;
                GridView1.Columns.Add(boundField);
            }

            GridView1.DataSource = dataTable;
            GridView1.DataBind();
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜