Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
this image describe what i have
XML
https://www.dropbox.com/s/nwjfhwxh35w088k/grid.png</a>
i want to disable editing on the column Item_IP

my gridview code
<pre lang="HTML">
<pre lang="xml"><asp:GridView ID="GridView1" runat="server"
                            CellPadding="4" ForeColor="#333333" GridLines="Vertical" Height="265px"
                            Width="100%" HorizontalAlign="Left" onrowediting="gridView_RowEditing"
            onrowcancelingedit="gridView_RowCancelingEdit"
            onrowupdating="gridView_RowUpdating" onrowdeleting="gridView_Rowdeleting">
                            <AlternatingRowStyle BackColor="White" />
                            <Columns>
     <asp:TemplateField>

      <ItemTemplate>
      <asp:Button ID="btnedit" CommandName="EDIT" runat="server" Text="EDIT" />
      </ItemTemplate>
      <EditItemTemplate>
      <asp:Button ID="btnupdate" CommandName="Update" runat="server" Text="Update" />
      <asp:Button ID="btnCancel" CommandName="Cancel" runat="server" Text="Cancel" />
      <asp:Button ID="btnDelete" CommandName="Delete" runat="server" Text="Delete" />

      </EditItemTemplate>

     </asp:TemplateField>
    </Columns>
                            <EditRowStyle BackColor="#2461BF" BorderWidth="1px" />
                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" Height="3px" />
                            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                            <RowStyle BackColor="#EFF3FB" />
                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                            <SortedAscendingCellStyle BackColor="#F5F7FB" />
                            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                            <SortedDescendingCellStyle BackColor="#E9EBEF" />
                            <SortedDescendingHeaderStyle BackColor="#4870BE" />
                        </asp:GridView>
</pre>


and the C# code
&lt;pre lang=&quot;c#&quot;&gt;

    public partial class _default : System.Web.UI.Page
    {

        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                dt = new DataTable();
                MakeTable();
            }
            else
            {
                dt = (DataTable)ViewState[&quot;dt&quot;];
            }
            ViewState[&quot;dt&quot;] = dt;

        }
        protected void MakeTable()
        {
            dt.Columns.Add(&quot;Item_IP&quot;);
            dt.Columns.Add(&quot;Name&quot;);
            dt.Columns.Add(&quot;Price&quot;);
        }




        protected void subCatClick(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            int item_ip = btn.TabIndex;
            try
            {
                OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
                myOleDbConnection.Open();
                OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
                myOleDbCommand.CommandText = &quot;select arabic_name,sale_price from item where ip=&quot; +item_ip;
                OleDbDataReader dr = myOleDbCommand.ExecuteReader();
                dr.Read();
                DataRow drow = dt.NewRow();
                drow[&quot;Item_IP&quot;] = item_ip.ToString();
                drow[&quot;Name&quot;] = dr[&quot;ARABIC_NAME&quot;].ToString();

                drow[&quot;Price&quot;] = dr[&quot;SALE_PRICE&quot;].ToString();

                dt.Rows.Add(drow);
                GridView1.DataSource = dt;
                GridView1.DataBind();

                dr.Close();
                myOleDbConnection.Close();

            }
            catch (Exception eee)
            {
                Response.Write(&quot;&amp;lt;script&gt;alert(&#39;error in subCatClick&#39;)&amp;lt;/script&gt;&quot;);
            }


        }
        protected void gridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        protected void gridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];


            string ip    = ((TextBox)row.Cells[1].Controls[0]).Text;


                string name = ((TextBox)row.Cells[2].Controls[0]).Text;

                string price = ((TextBox)row.Cells[3].Controls[0]).Text;



                DataRow[] datarow = dt.Select(&quot;Item_IP=&#39;&quot; + ip + &quot;&#39;&quot;);
                dt.Rows[e.RowIndex].BeginEdit();
                dt.Rows[e.RowIndex][&quot;Name&quot;] = name;

                dt.Rows[e.RowIndex][&quot;Price&quot;] = price;

                dt.Rows[e.RowIndex].EndEdit();
                dt.AcceptChanges();
                GridView1.EditIndex = -1;
                GridView1.DataSource = dt;
                GridView1.DataBind();


        }
        protected void gridView_Rowdeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            string ip = ((TextBox)row.Cells[1].Controls[0]).Text;
            string name = ((TextBox)row.Cells[2].Controls[0]).Text;
            DataRow[] datarow;
            datarow = dt.Select();
            datarow = dt.Select(&quot;Item_IP=&#39;&quot; + ip + &quot;&#39; AND Name=&#39;&quot;+ name +&quot;&#39;&quot;);
            foreach (DataRow r in datarow)
            { r.Delete(); break; }
            GridView1.EditIndex = -1;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

    }
}

&lt;/pre&gt;</pre>
Posted
Comments
M. Daban 26-Feb-14 10:03am    
image url: https://www.dropbox.com/s/nwjfhwxh35w088k/grid.png
JoCodes 26-Feb-14 23:20pm    
Specify whether you are creating template field or BoundField columns for the Gridview.Have you tried to set the Column to readonly in the RowDataBound event?

1 solution

Improved solution, tested in VS2010:

Since you already have ItemTemplate structure, one way to achieve this is to set the edit template for each of the columns, and have a non-editable field (a label) for the columns that you want to keep disabled from the users.

XML
<asp:GridView ID="GridView1" runat="server"
                            CellPadding="4" ForeColor="#333333" 
        GridLines="Vertical" Height="265px"
                            Width="100%" HorizontalAlign="Left" onrowediting="gridView_RowEditing"
            onrowcancelingedit="gridView_RowCancelingEdit"
            onrowupdating="gridView_RowUpdating" 
        onrowdeleting="gridView_Rowdeleting" AutoGenerateColumns="False">
                            <AlternatingRowStyle BackColor="White" />
                            <Columns>
                                 <asp:TemplateField ShowHeader="true">
 
                                      <ItemTemplate>
                                      <asp:Button ID="btnedit" CommandName="EDIT" runat="server" Text="EDIT" />
                                      </ItemTemplate>
                                      <EditItemTemplate>
                                      <asp:Button ID="btnupdate" CommandName="Update" runat="server" Text="Update" />
                                      <asp:Button ID="btnCancel" CommandName="Cancel" runat="server" Text="Cancel" />
                                      <asp:Button ID="btnDelete" CommandName="Delete" runat="server" Text="Delete" />
 
                                      </EditItemTemplate>
 
                                 </asp:TemplateField>
                                 <!--Note that both ItemTemplate and EditTemplate return a non-editable label here-->
                                 <asp:TemplateField HeaderText="Item IP" ShowHeader="true">
                                     <ItemTemplate>
                                         <asp:Label ID="iplabel" runat="server" Text='<%# Bind("Item_IP") %>'></asp:Label>
                                     </ItemTemplate>
                                     <EditItemTemplate>
                                         <asp:Label ID="iplabel" runat="server" Text='<%# Bind("Item_IP") %>'></asp:Label>
                                     </EditItemTemplate>
                                 </asp:TemplateField>
                                 <!--For a field in which editing is allowed, changed the edit template to a TextBox, and treat accordingly in your code behind.-->
                                 <asp:TemplateField HeaderText="Name" ShowHeader="true">
                                     <ItemTemplate>
                                         <asp:Label ID="namelabel" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                                     </ItemTemplate>
                                     <EditItemTemplate>
                                         <asp:TextBox ID="namebox" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                                     </EditItemTemplate>
                                 </asp:TemplateField>
                                 <asp:TemplateField HeaderText="Price" ShowHeader="true">
                                     <ItemTemplate>
                                         <asp:Label ID="pricelabel" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
                                     </ItemTemplate>
                                     <EditItemTemplate>
                                         <asp:TextBox ID="pricebox" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox>
                                     </EditItemTemplate>
                                 </asp:TemplateField>
                                 
                            </Columns>
                            <EmptyDataTemplate>
                                <p>No items.</p>
                            </EmptyDataTemplate>
                            <EditRowStyle BackColor="#2461BF" BorderWidth="1px" />
                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" Height="3px" />
                            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                            <RowStyle BackColor="#EFF3FB" />
                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                            <SortedAscendingCellStyle BackColor="#F5F7FB" />
                            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                            <SortedDescendingCellStyle BackColor="#E9EBEF" />
                            <SortedDescendingHeaderStyle BackColor="#4870BE" />
                        </asp:GridView>


The previous suggestion I made about instantiating and setting the columns individually was not enough to overcome the behavior of the GridView. I've tested this, though, and it appears to have the behavior you desire.
 
Share this answer
 
v3
Comments
M. Daban 27-Feb-14 4:00am    
Thank you, but it still editable by user.
i want to disable or remove the textbox in the column
and want to edit it programaticly
David Days 28-Feb-14 10:21am    
I changed the solution to put the definition in the markup. This should work for you.
M. Daban 1-Mar-14 4:53am    
Thank you very much
David Days 1-Mar-14 15:49pm    
Glad to help. Stop by any time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900