datagrid的自定义和隐藏栏

本文档展示了如何在ASP.NET中自定义DataGrid控件,包括添加、隐藏列以及处理用户交互。通过RadioButton控制列的可见性,使用BoundColumn和TemplateColumn展示数据。同时,解释了在用户点击button时如何触发特定事件(如ShowInfo),并从DataGrid中获取选定行的数据。在Page_Load事件中检查是否显示特定列,而在itemCommand事件中处理更多信息的展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<ASP:RadioButton id="chkVisible" GroupName="Col2Visible" runat="server"
                   AutoPostback="True" /> Visible &nbsp;
  <ASP:RadioButton id="chkNotVisible" GroupName="Col2Visible" runat="server"
                   AutoPostback="True" /> Hidden<p />

  <ASP:DataGrid id="MyDataGrid" runat="server"
       AutoGenerateColumns="False"   //注意这儿
       CellPadding="5"
       GridLines="None"
       HeaderStyle-BackColor="silver"
       HeaderStyle-HorizontalAlign="center"
       FooterStyle-BackColor="silver"
       ShowFooter="True"
       OnItemCommand="ShowInfo">
  <!--此处声明了itemCommand事件处理函数,用于处理button控件的点击-->
    <Columns>//用此元素指定要显示的栏

      <ASP:TemplateColumn HeaderText="" ItemStyle-BackColor="silver">
        <ItemTemplate>&nbsp;</ItemTemplate>
      </ASP:TemplateColumn>
       //第一栏为空
      <ASP:BoundColumn HeaderText="<b>Code</b>" DataField="ISBN" ItemStyle-BackColor="lightblue" />
      <ASP:BoundColumn HeaderText="<b>Book Title</b>" DataField="Title"/>
      <ASP:BoundColumn HeaderText="<b>Released</b>" DataField="PublicationDate" DataFormatString="{0:D}" ItemStyle-HorizontalAlign="right" ItemStyle-BackColor="yellow" />
   //使用datafield属性指定数据绑定源中的某个字段     
      <ASP:TemplateColumn HeaderText="" ItemStyle-BackColor="lightblue">
        <ItemTemplate>
          <ASP:Button id="cmdInfo" Text="More Info" CommandName="Info" runat="server" />
        </ItemTemplate>
      </ASP:TemplateColumn>
     //上面在第五栏放置asp:button,设置commmandnamw为info
      <ASP:TemplateColumn HeaderText="<b>Buy Now</b>" ItemStyle-BackColor="silver" ItemStyle-HorizontalAlign="center">
        <ItemTemplate>
          <ASP:CheckBox id="chkBuy" runat="server" />
        </ItemTemplate>
      </ASP:TemplateColumn>
   //此栏放置了checkbox。使用TemplateColumn和ItemTemplate可以添加不属于原来数据集部分的附加栏
    </Columns>
以下是脚本:
<script language="C#" runat="server">

 void Page_Load(Object sender, EventArgs e)
 {
  if (Page.IsPostBack)//判断是否显示第4栏(从1开始数)
  {
   // display or hide the "Release Date" column
   MyDataGrid.Columns[3].Visible = (chkVisible.Checked == true);
  }
  else
  {
   chkVisible.Checked = true;  // set default value
   BindDataGrid();     // create data set and bind grid
  }
 }

      //响应itemcommand事件
 //Object objSender是包含对触发该事件的控件的引用,DataGridCommandEventArgs objArgs对象包含该事件以及对该控件中当前行(即包含被激活的控件的行)的引用的详细资料。

 void ShowInfo(Object objSender, DataGridCommandEventArgs objArgs )
 {
  // runs when any command button in the grid is clicked
  // see if the CommandName of the clicked button was "Info"
  if (objArgs.CommandName == "Info")//使用commandname属性来判断是button
  {
   // get values of ISBN and Title from Text property of the table cells
   // for the current row returned in the objArgs parameter values
                       //得到被激发控件所在行的第二,三栏的值
   string strISBN = objArgs.Item.Cells[1].Text;
   string strTitle = objArgs.Item.Cells[2].Text;

   // display the informaion in the page - possibly extract from database?
   lblInfo.Text = "More information about the book:<br /><b>" + strTitle
       + "</b><br />(ISBN " + strISBN + ") goes here...";
  }
 }


 void BindDataGrid()
 {
  // get connection string from ../global/connect-strings.ascx user control
  string strConnect = ctlConnectStrings.OLEDBConnectionString;

  // create a SQL statement to select some rows from the database
  string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%18610023%'";

  // create a variable to hold an instance of a DataReader object
  OleDbDataReader objDataReader;

  try
  {
   // create a new Connection object using the connection string
   OleDbConnection objConnect = new OleDbConnection(strConnect);

   // open the connection to the database
   objConnect.Open();

   // create a new Command using the connection object and select statement
   OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect);

   // execute the SQL statement against the command to get the DataReader
   objDataReader = objCommand.ExecuteReader();
  }
  catch (Exception objError)
  {
   // display error details
   outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
        + objError.Message + "<br />" + objError.Source + "<p />";
   return;  // and stop execution
  }

  // set the DataSource property of the DataList
  MyDataGrid.DataSource = objDataReader;

  // and bind the control to the data
  MyDataGrid.DataBind();
 }

</script>

我的理解:当页面首次加载以及每次用户点击单选按钮时就会触发page_load,
但若是用户单击datagrid中的button时则仅仅是调用showinfo事件,并不触发page_load
MyDataGrid.Columns[3].Visible
objArgs.CommandName == "Info"
string strISBN = objArgs.Item.Cells[1].Text;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值