2016-06-09 65 views
1

我有一个从SqlDataSource填充特定值的GridView。将默认行添加到ASP.NET GridView顶部

如何添加默认行到myGridView的顶部?

而且我想要的默认行有文本“所有”

<asp:GridView ID="gvPoints" 
    runat="server" 
    DataSourceID="SqlDataSource_userPoints" 
    AllowPaging="True" 
    AutoGenerateColumns="False" 
    DataKeyNames="id_point,title" 
    BorderWidth="0px" 
    Width="100%" 
    GridLines="None"> 

    <Columns> 
     <asp:ButtonField 
      CommandName="selectedPoint" 
      DataTextField="title" 
      ButtonType="Link" /> 
    </Columns> 

    <RowStyle HorizontalAlign="Left"></RowStyle> 
</asp:GridView> 



<asp:SqlDataSource 
    ID="SqlDataSource_userPoints" 
    runat="server" 
    ConnectionString='<%$ ConnectionStrings:TipTourConnectionString %>' 
    SelectCommand ="SELECT 
         id_point, 
         title, 
         body 

        FROM tt_point 

        WHERE id_user = @id_user"> 

    <SelectParameters> 
     <asp:QueryStringParameter 
      QueryStringField="id_user" 
      DefaultValue="0" 
      Name="id_user"> 
     </asp:QueryStringParameter> 
    </SelectParameters> 
</asp:SqlDataSource> 

enter image description here

回答

1

我认为在这种情况下最简单的方法是用SQL语句添加。

这里假设id_point是一个数字字段。如果没有,删除转换功能

SELECT 'All' AS id_point, 'All' AS title, 'All' AS body 
UNION 
SELECT CONVERT(varchar, id_point), title, body FROM tt_point WHERE id_user = @id_user 
+0

谢谢,那会 将工作。 – blitzeus

+0

不管怎么说,有人可以解释如何用标记或代码背后的代码 – blitzeus

+1

这可能是最简单的方法,再次感谢@Clint B – blitzeus

1

更换这

<asp:ButtonField CommandName="selectedPoint" DataTextField="title" ButtonType="Link" /> 

与此

<asp:ButtonField CommandName="selectedPoint" DataTextField="title" ButtonType="Link" HeaderText="All" /> 

我添加HeaderText="All"ButtonField 确保在你的GridView这是有ShowHeader="true"

如果要添加2行绑定你的GridView这样

strcon = "data source=.\\SQLEXPRESS;database=testDB;trusted_Connection=yes";  
private void BindGridData() 
    { 
     SqlConnection connection = new SqlConnection(strcon); 
     SqlCommand command = new SqlCommand("SELECT id_point,title,body FROM tt_point WHERE id_user = @id_user", connection); 
     string userid = "0"; 
     command.Parameters.AddWithValue("@id_user", userid); 
     SqlDataAdapter daimages = new SqlDataAdapter(command); 
     DataTable dt = new DataTable(); 
     daimages.Fill(dt); 
     DataRow dr = dt.NewRow(); 
     dr["id_point"] = "All"; 
     dr["title"] = "All"; 
     dr["body"] = "All"; 
     dt.Rows.InsertAt(dr, 0); 
     DataRow dr1 = dt.NewRow(); 
     dr1["id_point"] = "All"; 
     dr1["title"] = "All"; 
     dr1["body"] = "All"; 
     dt.Rows.InsertAt(dr1, 1);    
     gvPoints.DataSource = dt; 
     gvPoints.DataBind(); 
    } 

呼叫在页面加载这个方法是这样

protected void Page_Load(object sender, EventArgs e) 
    {   
     if (!Page.IsPostBack) 
     { 
      BindGridData(); 
     } 
    } 

在这里,在strcon数据库名称是testDB替换为您数据库名称

从gridview删除这个DataSourceID =“SqlDataSource_userPoints”

+0

,这使得它看起来更像是一个标题标题,如果我有2个默认值来添加 – blitzeus

+0

@blitzeus 2默认值,你是指排? – Ansari

+0

@Ansari ...是的,我如何将新行添加到gridview的顶部 – blitzeus