Gridview操作

时间:2013-07-30 17:53:11

标签: c# asp.net gridview

请帮助我一个人,我在asp.net(C#代码)中有一个gridview,其中两列名为'Type',另一列名为'Save'。在'Type'列中,一些数据类似于定量和定性。因此,如果数据在“类型”列中是定量的,那么同一行中“保存”列中的相应单元格应该是DropdownList,如果它是定性的,那么列的相应单元格即同一行中的“保存”将是TextBox。

先谢谢

2 个答案:

答案 0 :(得分:1)

在“保存”列中,创建一个模板字段,并同时放置Textbox和Dropdown,然后当每一行都是数据绑定时,运行一个函数来检查Type列中的数据类型并隐藏(为此row)保存列中不需要的元素:

这是代码的样子:

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        bool isQuantitative = ((CheckBox)e.Row.FindControl("cb1")).Checked;

        if (isQuantitative)
        {
            ((textBox)e.Row.FindControl("myTextboxID")).Visible = true;
            ((DropDownList)e.Row.FindControl("myDdlistID")).Visible = false;
        }
        else
        {
            ((textBox)e.Row.FindControl("myTextboxID")).Visible = false;
            ((DropDownList)e.Row.FindControl("myDdlistID")).Visible = true;
        }
    }
}

上述示例中用于检查定量/定性数据的控件是一个复选框,将其替换为您的控件或逻辑。

然后将gridview链接到此方法,将此属性添加到gridview声明:

OnRowDataBound="myGridView_RowDataBound"

答案 1 :(得分:0)

我正在寻找你的程序数据源的回复。无论如何,我已经为你创建了这个演示。这是你的完整答案。

这是您的页面:

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="EnableAndDisableControlsGridviewWebApp._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <div>
        <asp:GridView ID="GridView1" AutoGenerateColumns="false"
                      runat="server" onrowdatabound="GridView1_RowDataBound">
            <RowStyle BackColor="#EFF3FB" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="Id" Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblId" Style="width: 100px;" runat="server"
                            Text='<%# Eval("Id")%>' Visible="false"></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                      <asp:Label ID="lblId" Style="width: 100px;" runat="server"
                            Text='<%# Eval("Id")%>' Visible="false"></asp:Label>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Type">
                    <ItemTemplate>
                        <asp:Label ID="lblCustomType" Style="width: 100px;" runat="server"
                            Text='<%# Eval("CustomType")%>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                           <asp:Label ID="lblCustomType" Style="width: 100px;" runat="server"
                            Text='<%# Eval("CustomType")%>'></asp:Label>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Details">
                    <ItemTemplate>
                        <asp:TextBox ID="txtSave" Style="width: 100px;" runat="server"></asp:TextBox>
                        <asp:DropDownList ID="DrpSave" Style="width: 100px;" runat="server"></asp:DropDownList>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtSave" Style="width: 100px;" runat="server"></asp:TextBox>
                        <asp:DropDownList ID="DrpSave" Style="width: 100px;" runat="server"></asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
</asp:Content>

你的代码就是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

namespace EnableAndDisableControlsGridviewWebApp
{
    public partial class _Default : System.Web.UI.Page
    {
        DataTable aTable = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            aTable.Columns.Add("Id", typeof(int));
            aTable.Columns.Add("CustomType", typeof(string));
            aTable.Columns.Add("CustomSave", typeof(string));
            DataRow dr1 = aTable.NewRow();
            dr1["Id"] = 1;
            dr1["CustomType"] = "qualitative randomly";
            dr1["CustomSave"] = "DropdownList";
            aTable.Rows.Add(dr1);
            DataRow dr2 = aTable.NewRow();
            dr2["Id"] = 2;
            dr2["CustomType"] = "quantitative";
            dr2["CustomSave"] = "TextBox";
            aTable.Rows.Add(dr2);
            DataRow dr3 = aTable.NewRow();
            dr3["Id"] = 3;
            dr3["CustomType"] = "qualitative randomly";
            dr3["CustomSave"] = "DropdownList";
            aTable.Rows.Add(dr3);
            DataRow dr4 = aTable.NewRow();
            dr4["Id"] = 4;
            dr4["CustomType"] = "quantitative";
            dr4["CustomSave"] = "TextBox";
            aTable.Rows.Add(dr4);
            GridView1.DataSource = aTable;
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblIdentifier = (Label)e.Row.FindControl("lblCustomType");
                string val = lblIdentifier.Text;

                if (val == "quantitative")
                {
                    ((TextBox)e.Row.FindControl("txtSave")).Visible = true;
                    ((DropDownList)e.Row.FindControl("DrpSave")).Visible = false;
                }
                else
                {
                    ((TextBox)e.Row.FindControl("txtSave")).Visible = false;
                    ((DropDownList)e.Row.FindControl("DrpSave")).Visible = true;
                }
            }
        }
    }           
}