如何在gridview内的特定列中禁用编辑 [英] How to disable editing in specific column inside gridview

查看:118
本文介绍了如何在gridview内的特定列中禁用编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此图片描述了我的内容

 https://www.dropbox.com/s/nwjfhwxh35w088k/grid.png < span class =code-keyword><   / a  >  
i想要禁用列上的编辑Item_IP

我的gridview代码
< pre lang = HTML >
< pre lang = xml > < asp:GridView ID = GridView1 < span class =code-attribute> runat = server

CellPadding = 4 ForeColor = #333333 GridLines = 垂直 高度 < span class =code-keyword> = 265px

< span class =code-attribute> 宽度 = 100% Horizo​​ntalAlign = onrowediting = gridView_RowEditing

onrowcancelingedit = gridView_RowCancelingEdit

onrowupdating = gridView_RowUpdating onrowdeleting <温泉n class =code-keyword> = gridView_Rowdeleting >
< AlternatingRowStyle BackColor = 白色 / >
< >
< asp:TemplateField >

< ItemTemplate >
< asp:按钮 ID = btnedit CommandName = 编辑 < span class =code-attribute> runat = server 文本 = 编辑 / >
< / ItemTemplate >
< EditItemTemplate >
< < span class =code-leadattribute> asp:按钮 ID = btnupdate CommandName = 更新 runat = server < span class =code-attribute> 文本 = 更新 / > ;
< asp:按钮 ID = btnCancel CommandName = 取消 runat = server 文本 = 取消 / >
< asp:按钮 ID = btnDele te CommandName = 删除 runat = server 文字 = 删除 / >

< / EditItemTemplate >

< / asp:TemplateField >
< /列 >
< EditRowStyle BackColor = #2461BF BorderWidth = 1px / < span class =code-keyword>>
< FooterStyle BackColor = #507CD1 Font-Bold = True ForeColor = 白色 / < span class =code-keyword>>
< HeaderStyle BackColor = #507CD1 Font-Bold = True ForeColor = 白色 高度 = < span class =code-keyword> 3px / >
< PagerStyle BackColor = #2461BF ForeColor = 白色 Horizo​​ntalAlign = 中心 / >
< RowStyle BackColor = #EFF3FB / >
< SelectedRowStyle < span class =code-attribute> BackColor = #D1DDF1 Font-Bold = True ForeColor = #333333 / >
< SortedAscendingCellStyle BackColor = #F5F7FB / >
< SortedAscendingHeaderStyle BackColor = #6D95E1 < span class =code-keyword> / >
< SortedDescendingCellStyle BackColor = #E9EBEF / >
< SortedDescendingHeaderStyle BackColor = #4870BE / >
< / asp:GridView >
< / pre >


和C#代码
& 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();
}
其他
{
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)发件人;
int item_ip = btn.TabIndex;
try
{
OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
myOleDbConnection.Open();
OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
myOleDbCommand.CommandText =& quot;从项目中选择arabicprice,sale_price,其中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(卓尔);
GridView1.DataSource = dt;
GridView1.DataBind();

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

}
catch(例外eee)
{
Response.Write(& quot;& amp; lt; script& gt; alert(&#) 39; 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中的DataRow r)
{r.Delete();打破; }
GridView1.EditIndex = -1;
GridView1.DataSource = dt;
GridView1.DataBind();
}

}
}

& lt; / pre& gt; < / pre >

解决方案

改进的解决方案,在VS2010中测试过:



既然你已经有了ItemTemplate结构,一种方法就是设置每个列的编辑模板,并为要保持对用户禁用的列具有不可编辑的字段(标签)。



< pre lang =xml> < asp:GridView ID = GridView1 runat = server

< span class =code-attribute> CellPadding = 4 < span class =code-attribute> ForeColor = #333333

GridLines = 垂直 高度 = 265px
< span class =code-attribute>
< span class =code-attribute> 宽度 = 100% Horizo​​ntalAlign = onrowediting = gridView_RowEditing

onrowcancelingedit = < span class =code-keyword> gridView_RowCancelingEdit

< span class =code-attribute> onrowupdating = gridView_RowUpdating

onrowdeleting = gridView_Rowdeleting AutoGenerateColumns = False >
< AlternatingRowStyle BackColor = 白色 / >
< < span class =code-keyword>>
< asp:TemplateField ShowHeader = true >

< ItemTemplate >
< asp:按钮 ID = btnedit CommandName = 编辑 runat = server 文本 = 编辑 / >
< / ItemTemplate >
< EditItemTemplate中 <跨度class =code-keyword>>
< 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> $b $b <!--Note that both ItemTemplate and EditTemplate return a non-editable label here-->
<asp:TemplateField HeaderText=\"Item IP\" ShowHeader=\"true\">
<ItemTemplate>
&l t;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=\"namebo x\" runat=\"server\" Text='<%# Bind(\"Name\") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField> $ b$b <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.


this image describe what i have

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>

解决方案

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.

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


这篇关于如何在gridview内的特定列中禁用编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆