ButtonColumn.Visible=false
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>
<script runat="server">
ICollection CreateDataSource()
{
// Create sample data for the DataGrid control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
// Populate the table with sample values.
for (int i = 0; i < 5; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
} } void Button_Click(Object sender, EventArgs e)
{ // Count the number of selected items in the DataGrid control.
int count = 0; // Display the selected times.
Message.Text = "You Selected: <br>"; // Iterate through each item (row) in the DataGrid control and
// determine whether it is selected.
foreach (DataGridItem item in ItemsGrid.Items)
{ DetermineSelection(item, ref count); } // If no items are selected, display the appropriate message.
if (count == 0)
{ Message.Text = "No items selected"; } } void DetermineSelection(DataGridItem item, ref int count)
{ // Retrieve the SelectCheckBox CheckBox control from the
// specified item (row) in the DataGrid control.
CheckBox selection = (CheckBox)item.FindControl("SelectCheckBox"); // If the item is selected, display the appropriate message and
// increment the count of selected items.
if (selection != null)
{ if (selection.Checked)
{
Message.Text += "- " + item.Cells[1].Text + "<br>";
count++;
} } } void Check_Change(Object sender, EventArgs e)
{ // Show or hide the first column depending on the value of
// the check box.
if (ShowCheckBox.Checked)
{
ItemsGrid.Columns[0].Visible = true;
}
else
{
ItemsGrid.Columns[0].Visible = false;
} } </script>
<body>
<form runat=server>
<h3>DataGridColumn Visible Example</h3> Select whether to show or hide the first column. <br><br>
<b>Product List</b>
<asp:DataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
ShowFooter="True"
AutoGenerateColumns="False"
runat="server"> <HeaderStyle BackColor="#00aaaa">
</HeaderStyle> <FooterStyle BackColor="#00aaaa">
</FooterStyle> <Columns> <asp:BoundColumn DataField="IntegerValue"
Visible="True"
HeaderText="Item"/> <asp:BoundColumn DataField="StringValue"
Visible="True"
HeaderText="Description"/> <asp:BoundColumn DataField="CurrencyValue"
Visible="True"
HeaderText="Price"
DataFormatString="{0:c}"> <ItemStyle HorizontalAlign="Right">
</ItemStyle> </asp:BoundColumn> <asp:TemplateColumn HeaderText="Select Item"
Visible="True" > <ItemTemplate> <asp:CheckBox id="SelectCheckBox"
Text="Add to Cart"
Checked="False"
runat="server"/> </ItemTemplate> </asp:TemplateColumn>
</Columns>
</asp:DataGrid> <br><br> <asp:Button id="SubmitButton"
Text="Submit"
OnClick = "Button_Click"
runat="server"/> <br><br> <asp:Label id="Message"
runat="server"/> <hr> <asp:CheckBox id="ShowCheckBox"
Text="Show first column"
AutoPostBack="True"
OnCheckedChanged="Check_Change"
Checked="True"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>
<script runat="server">
ICollection CreateDataSource()
{
// Create sample data for the DataGrid control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
// Populate the table with sample values.
for (int i = 0; i < 5; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
void Page_Load(Object sender, EventArgs e)
{
// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
} } void Button_Click(Object sender, EventArgs e)
{ // Count the number of selected items in the DataGrid control.
int count = 0; // Display the selected times.
Message.Text = "You Selected: <br>"; // Iterate through each item (row) in the DataGrid control and
// determine whether it is selected.
foreach (DataGridItem item in ItemsGrid.Items)
{ DetermineSelection(item, ref count); } // If no items are selected, display the appropriate message.
if (count == 0)
{ Message.Text = "No items selected"; } } void DetermineSelection(DataGridItem item, ref int count)
{ // Retrieve the SelectCheckBox CheckBox control from the
// specified item (row) in the DataGrid control.
CheckBox selection = (CheckBox)item.FindControl("SelectCheckBox"); // If the item is selected, display the appropriate message and
// increment the count of selected items.
if (selection != null)
{ if (selection.Checked)
{
Message.Text += "- " + item.Cells[1].Text + "<br>";
count++;
} } } void Check_Change(Object sender, EventArgs e)
{ // Show or hide the first column depending on the value of
// the check box.
if (ShowCheckBox.Checked)
{
ItemsGrid.Columns[0].Visible = true;
}
else
{
ItemsGrid.Columns[0].Visible = false;
} } </script>
<body>
<form runat=server>
<h3>DataGridColumn Visible Example</h3> Select whether to show or hide the first column. <br><br>
<b>Product List</b>
<asp:DataGrid id="ItemsGrid"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
ShowFooter="True"
AutoGenerateColumns="False"
runat="server"> <HeaderStyle BackColor="#00aaaa">
</HeaderStyle> <FooterStyle BackColor="#00aaaa">
</FooterStyle> <Columns> <asp:BoundColumn DataField="IntegerValue"
Visible="True"
HeaderText="Item"/> <asp:BoundColumn DataField="StringValue"
Visible="True"
HeaderText="Description"/> <asp:BoundColumn DataField="CurrencyValue"
Visible="True"
HeaderText="Price"
DataFormatString="{0:c}"> <ItemStyle HorizontalAlign="Right">
</ItemStyle> </asp:BoundColumn> <asp:TemplateColumn HeaderText="Select Item"
Visible="True" > <ItemTemplate> <asp:CheckBox id="SelectCheckBox"
Text="Add to Cart"
Checked="False"
runat="server"/> </ItemTemplate> </asp:TemplateColumn>
</Columns>
</asp:DataGrid> <br><br> <asp:Button id="SubmitButton"
Text="Submit"
OnClick = "Button_Click"
runat="server"/> <br><br> <asp:Label id="Message"
runat="server"/> <hr> <asp:CheckBox id="ShowCheckBox"
Text="Show first column"
AutoPostBack="True"
OnCheckedChanged="Check_Change"
Checked="True"
runat="server"/>
</form>
</body>
</html>
解决方案 »
- 提交数据都后台生成一个excel文件后怎么弹出另存为菜单保存文件
- 请帮忙推荐一个网站源代码
- JS如何给ASPX.CS文件发送参数 并回调给JS呢
- 求教AccessDataSource的连接字符串暴露问题
- 100分,求Page_Init(……)不用手打,类似于双击按钮产生的方法。初学,说的有些乱。
- asp.net快捷方式
- 如何实现下面伸缩效果的tree(求代码)!急!!
- textbox属性设为ReadOnly的时候不能用RequiredFieldValidator控件?
- C#如何一个视频的周统计,月统计,年统计
- 调查 明天是8月15 单位都发东西了吗?
- 水晶报表部署问题
- 没有ASP基础,如何学习ASP.NET
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<script runat="server">
Public dim MyName,HisName as string
Sub Page_Load(Sender As Object, E As EventArgs)
MyName="admin"
HisName="nhonline"
Dim MyConnection as OleDbConnection = New OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\nhonline\datagrid\kk.mdb") Dim myCommand As OleDbCommand = new OleDbCommand("Select * From board", myConnection)
Dim myDataReader As OleDbDataReader Try
myConnection.Open() 'myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
myDataReader = myCommand.ExecuteReader
myDataGrid.DataSource = myDataReader
myDataGrid.DataBind()
Catch myException As Exception
Response.Write("An error has occurred: " & myException.ToString())
Finally
If Not myDataReader Is Nothing Then
myDataReader.Close()
End If
End Try
End SubSub HideShow_Click(Sender As Object, E As EventArgs)
If myDataGrid.Columns(0).Visible = False Then
myDataGrid.Columns(0).Visible = True
Else
myDataGrid.Columns(0).Visible = False
End If
End Sub
</script>
<body>
<form runat="server">
<asp:Button id="HideShow" Text="Hide/Show" OnClick="HideShow_Click" runat="server" /> <A HREF="datagrid_hidecolumn_2.aspx?name=<%=Myname%>">MyName=admin(Can Hidden)</A> <A HREF="datagrid_hidecolumn_2.aspx?name=<%=Hisname%>">HisName=nhonline</A><BR><hr color=red>
<asp:DataGrid id="myDataGrid" Width="25%" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Board_ID">
<ItemTemplate>
<span><%# Container.DataItem("boardid") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="boardtype">
<ItemTemplate>
<span><%# Container.DataItem("boardtype") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="class">
<ItemTemplate>
<span><%# Container.DataItem("class") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="readme">
<ItemTemplate>
<span><%# Container.DataItem("readme") %></span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="boardmaster">
<ItemTemplate>
<span><%# Container.DataItem("boardmaster") %></span>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
this.DataGrid1.Columns["列名或列序号"].Visible=false;