日期:2014-05-17  浏览次数:20896 次

c#学习笔记(1)GridView问题

(1)Gridview隐藏列

在网上我查到了两种解决办法

一种是:

在RowCreated事件中书写如下代码

Public  void myTestFunction()
{
  string conString="....";//省略
    string sqlquery="...";//省略
   SqlConnection con = new SqlConnection(conString);
        SqlDataAdapter da = new SqlDataAdapter(sqlquery, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ds.Tables[0].Columns[0].ColumnMapping = MappingType.Hidden;
        GridView1.DataSouce = ds.Tables[0];
        GridView1.DataBind() ;
 
}


Public  void myTestFunction()
{
  string conString="....";//省略
    string sqlquery="...";//省略
   SqlConnection con = new SqlConnection(conString);
        SqlDataAdapter da = new SqlDataAdapter(sqlquery, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ds.Tables[0].Columns[0].ColumnMapping = MappingType.Hidden;
        GridView1.DataSouce = ds.Tables[0];
        GridView1.DataBind() ;
 
}


(2)GridView超链接

<asp:GridView ID="CommonUrl" Width="99%" runat="server" 
            BackColor="#FFF7E7" BorderColor="#DEBA84" Font-Size="12px"
            BorderStyle="None" BorderWidth="1px" 
            AutoGenerateColumns="False"  AllowPaging="True" 
            OnRowDataBound="CommonUrl_RowDataBound">
        	
            <FooterStyle BackColor="#F7DFB5" ForeColor="#000066" />
            <RowStyle ForeColor="#000066" BackColor="#BFE6F9"   HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#738A9C" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#66CCFF"  HorizontalAlign="Center"/>
            
            <Columns>                      
                 <asp:TemplateField HeaderText="常用连接">                        
                          <ItemTemplate>    
                                <asp:HyperLink ID="CommonUrl" runat="server" Text='<%#Eval("Name") %>' NavigateUrl='<%#Eval("Url") %>' Target="_blank"></asp:HyperLink>
                         </ItemTemplate>
                </asp:TemplateField>        
            </Columns>
            
        </asp:GridView>	
超链接代码:
<a href="<%#Eval("Url")%>" target="_blank" ><%#Eval("Name")%></a>

<asp:HyperLink ID="CommonUrl" runat="server" Text='<%#Eval("Name") %>' NavigateUrl='<%#Eval("Url") %>'