我有一个从具有特定值的SqlDataSource填充的GridView。
如何在myGridView的顶部添加默认行?
我希望默认行包含文本'All'
<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>
答案 0 :(得分: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
答案 1 :(得分: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”