这是我的Html标记
<form runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2" Font-Names = "Arial" Font-Size = "10pt"
HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" DataKeyNames="FilePath" >
<Columns>
<asp:BoundField DataField="FileName" HeaderText="FileName" />
<asp:TemplateField>
<ItemTemplate>
<object type="application/x-shockwave-flash" data='dewplayer-vol.swf?mp3=<%#Eval("FilePath") %>'
width="240" height="20" id="dewplayer">
<param name="wmode" value="transparent" />
<param name="movie" value='dewplayer-vol.swf?mp3=<%#Eval("FilePath") %>' />
</object>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField><asp:TemplateField HeaderText="Song Ratings">
<ItemTemplate>
<asp:Rating ID="Rating1" OnChanged="OnRatingChanged" runat="server"
StarCssClass="Star" WaitingStarCssClass="WaitingStar" EmptyStarCssClass="Star"
FilledStarCssClass="FilledStar" CurrentRating='<%# Eval("Rating") %>'>
</asp:Rating>
</ItemTemplate>
</asp:TemplateField></Columns></asp:GridView></form>
这是我的aspx.cs文件
绑定ASP.Net GridView
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridviewData();
GridView1.DataSource = GetData("SELECT SongId, FileName, ISNULL((SELECT AVG(Rating) FROM SongRating WHERE SongId = SongTable.SongId), 0) Rating FROM SongTable");
GridView1.DataBind();
}
}
private static DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
//将用户评级插入并保存到SQL Server数据库表
protected void OnRatingChanged(object sender, RatingEventArgs e)
{
int rowIndex = ((sender as Rating).NamingContainer as GridViewRow).RowIndex;
int songId = Convert.ToInt32(GridView1.DataKeys[rowIndex].Value);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO SongRating VALUES(@SongId, @Rating)"))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@SongId", songId);
cmd.Parameters.AddWithValue("@Rating", e.Value);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
private void BindGridviewData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select * from SongTable", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
发生错误的地方我是asp.net的新手谢谢
答案 0 :(得分:1)
嗨@abhi你没有在你的选择列表中提到“评级”栏,做这样的事情
SELECT SongId,FileName,ISNULL(AVG(Rating),0)作为SongTable的评级左边加入SongRating,SongRating.SongId = SongTable.SongId group by SongId,FileName
希望这会对你有帮助..
答案 1 :(得分:0)
根据您的例外,它说
当Web中的指定数据项发生时,会发生上述异常 控件在返回的DataRowView的模式中不存在。在 换句话说,您使用数据绑定指定的列 从数据库
返回的数据中不存在控制
因此可能会在 BindGridviewData 方法
中更改您的选择查询select * from SongTable
到这个
SELECT SongId, FileName, ISNULL((SELECT AVG(Rating)
FROM SongRating WHERE SongId = SongTable.SongId), 0) as Rating FROM SongTable