views:

224

answers:

1

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"&gt;

<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.

+1  A: 

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();
        }
Saar
Well, it is not working for me. Can you give another hand?
JMSA
at which step it's not working
Saar
When I press Edit command, the 3rd and 5th cells should not be converted into textbox. I.e. they should remain uneditable.
JMSA
try with protected void Page_Load(object sender, EventArgs e) { CreateGridView(); }
Saar
But this is working: if (colCount == 3 || colCount == 5) { boundField.ReadOnly = true; }
JMSA
OK. Well done though.
JMSA
good that you played with the code. :)
Saar