首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎么使gridview的某一行不可用或者使gridview的某一个单元格不可用(要考虑模板列),先多谢大家了!

2012-03-22 
如何使gridview的某一行不可用或者使gridview的某一个单元格不可用(要考虑模板列),急啊,先谢谢大家了!!!我

如何使gridview的某一行不可用或者使gridview的某一个单元格不可用(要考虑模板列),急啊,先谢谢大家了!!!
我在使用gridview时要实现这样一个功能,要使某一行显示不可用,但是随便怎么样都没试出来,还有我要使某一单元格显示不可用,而这个单元格是模板列,我也没有成功,哪位高手能指点一下啊,先谢了!!!

[解决办法]

GridView1.Columns[0].Visible = false;

[解决办法]
显示不可用是什么意思?是用户准备编辑时提示不可用呢?还是干脆把某些行或列隐藏起来不显示?
[解决办法]
既然是不可用那肯定有个字段可以说吗的吧
把哪个字段读出来,但可以不要显示,在点击的时候取出来判断就可以了
[解决办法]
以Northwind数据库的Products表为例,价格(UnitPrice)小于50的不显示,价格大于100的不允许编辑。

HTML code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"             DataKeyNames="ProductID" DataSourceID="SqlDataSource1"             onrowdatabound="GridView1_RowDataBound" onrowediting="GridView1_RowEditing">            <Columns>                <asp:CommandField ShowEditButton="True" />                <asp:BoundField DataField="ProductID" HeaderText="ProductID"                     InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />                <asp:TemplateField HeaderText="ProductName" SortExpression="ProductName">                    <ItemTemplate>                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>                    </ItemTemplate>                    <EditItemTemplate>                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>                    </EditItemTemplate>                </asp:TemplateField>                <asp:TemplateField HeaderText="UnitPrice" SortExpression="UnitPrice">                    <ItemTemplate>                        <asp:Label ID="LabelUnitPrice" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:Label>                    </ItemTemplate>                    <EditItemTemplate>                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:TextBox>                    </EditItemTemplate>                </asp:TemplateField>            </Columns>        </asp:GridView>        <asp:SqlDataSource ID="SqlDataSource1" runat="server"             ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"             DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID"             InsertCommand="INSERT INTO [Products] ([ProductName], [UnitPrice]) VALUES (@ProductName, @UnitPrice)"             SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]"             UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [UnitPrice] = @UnitPrice WHERE [ProductID] = @ProductID">            <DeleteParameters>                <asp:Parameter Name="ProductID" Type="Int32" />            </DeleteParameters>            <UpdateParameters>                <asp:Parameter Name="ProductName" Type="String" />                <asp:Parameter Name="UnitPrice" Type="Decimal" />                <asp:Parameter Name="ProductID" Type="Int32" />            </UpdateParameters>            <InsertParameters>                <asp:Parameter Name="ProductName" Type="String" />                <asp:Parameter Name="UnitPrice" Type="Decimal" />            </InsertParameters></asp:SqlDataSource> 


[解决办法]
测试发现Row_DataBound里的
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow row = e.Row;
string s = ((Label)row.FindControl("LabelUnitPrice")).Text;
if (!string.IsNullOrEmpty(s) && decimal.Parse(s) < 50)
row.Visible = false;
}
这部分会出现问题,因为在进入编辑模式时也会触发Row_DataBound事件,那么这里就不可能找到LabelUnitPrice了。

改了一下代码,规则是1、不显示价格小于50的行;2、价格大于100的行不允许进入编辑状态;3、价格大于70的行允许进入编辑状态,但不能修改价格(UnitPrice)列的值。

C# code
protected void Page_Load(object sender, EventArgs e){        if (!IsPostBack)            ViewState["InEditMode"] = false; //用于跟踪是否在编辑模式下。}protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {        if (e.Row.RowType == DataControlRowType.DataRow)        {            DataRowView drv = (DataRowView)e.Row.DataItem;            if (!Convert.IsDBNull(drv["UnitPrice"]) && Convert.ToDecimal(drv["UnitPrice"]) < 50)                e.Row.Visible = false;        }        if ((bool)ViewState["InEditMode"] == true) //如果在编辑模式下        {            TextBox tb = e.Row.FindControl("TextBox2") as TextBox; //在编辑模式下才能找到TextBox2,也就是输入UnitPrice的TextBox            if (tb != null)            {                if (decimal.Parse(tb.Text) > 70) //如果价格大于70,则设置为只读                    tb.ReadOnly = true;            }        }                }protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)    {        GridViewRow row = GridView1.Rows[e.NewEditIndex];        string s = ((Label)row.FindControl("LabelUnitPrice")).Text;                if (decimal.Parse(s) > 100)        {            Literal msg = new Literal();            msg.Text = "<script>alert('价格大于100,不能编辑此行');</script>";            this.Controls.Add(msg);            e.Cancel = true;            e.NewEditIndex = -1;        }        ViewState["InEditMode"] = true; // 设置进入编辑模式。        } 

热点排行