开发者

gridview Binddata

I'm working with a nested grid view. The second one should be bound b开发者_JAVA百科y using a stored procedure. The parameter for the stored procedure is gridview1.selected value and the connection string should be taken from config.connection string. My code is:

SqlCommand cmd = 
    new SqlCommand("spname", new SqlConnection(Config.connectionstring));
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("parametername", GridView1.SelectedValue);
cmd.Connection.Open();
control.DataSource = cmd.ExecuteReader();
control.DataBind();

But it doesn't work. Any ideas, please?


You have to code this on the previous gridview event of databounding, in this event you have to first find the control and then bound the data.


To me it looks like there is a flaw in your code.

1: You are trying to pass GridView1.SelectedValue but upon first run, there will be no SelectedRow unless you are setting it explicitly.

2: Each time the gv2 is databound it will be using same parameter value because at any point there will be only one SelectedRow and so SelectedValue passed will be same.

So please share more code and if possible some screen shot of your expected result.


The RowDataBound event fires when Rows bind the data. You have to do it like...

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
    System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;

        String x = dr["yourColumnName"].ToString();
        GridView gridView2 = (GridView)e.Row.Findcontrol("gridView2");//add this
        gridView2.DataSource = Assign Data source here;
        gridView2.DataBind();

}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜