如何根据我在Gridview控件中选择的值从数据库检索数据到Gridview?

时间:2010-01-29 06:33:59

标签: asp.net asp.net-3.5

Hai Pupils,

     I am a beginner in Dot net Domain.Presently Im creating a web portal for one of our client.In my portal i have displayed the News headline and a button for details in a Grid view. when i click on the button it has to show the detailed news from DB in another grid view. I have done a code on it.,but it shows some error.I have displayed the code below.Please help me to overcome this problem.

我的代码是

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)     {         if(e.Row.RowType!= DataControlRowType.Header&& e.Row.RowType!= DataControlRowType.Footer&& e.Row.RowType!= DataControlRowType.Pager)         {             按钮btn =(按钮)e.Row.FindControl(“Button12”);             btn.CommandArgument = e.Row.RowIndex.ToString();         }     }     protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)     {         if(e.CommandName ==“newsbtn”)         {             int index = int.Parse(e.CommandArgument.ToString());             string key = GridView2.DataKeys [index] .Value.ToString();

        SqlDataAdapter da = new SqlDataAdapter("select **img,news,detail** from News where news=@n", connect.con());
        da.SelectCommand.Parameters.AddWithValue("@n", key);
        da.Fill(ds, "more");

        //if (!IsPostBack)
        //{
            GridView1.DataSource = ds;
            GridView1.DataBind();
        //}
    }
}

它显示的错误是:

DataBinding:'System.Data.DataRowView'不包含名称为'detail'的属性。

1 个答案:

答案 0 :(得分:0)

可能这些将有助于 1. Master/Detail Using a Selectable Master GridView with a Details DetailView使用Gridview而不是DetailsView来显示子记录
2.或者你可以尝试更高级的方法GridView control to show master-child or master-slave data, written in c#, asp.net, and javascript

干杯