select query for each record of Dataset then bind in Gridview

Posted by Vanchi050585 under ASP.NET on 6/10/2010 | Views : 7722 | Status : [Member] | Replies : 1
hi friends,

i wants to display the related informations of each record of dataset. That mean, dataset having rows for A,B,C and etc..
Then i wants to display the information in same Gridview of each record(A,B,C). i wrote the below my coding, it dispaly info of the last row of dataset only. pls correct the coding.


protected void btnClick_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strCS);
string strQury = "select [Group] from tblCust1 where CustID='"+ddlCustID.SelectedItem.ToString()+"'";
SqlDataAdapter da = new SqlDataAdapter(strQury, con);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
strGroup = row["Group"].ToString();
BindGridview(strGroup,txtDate.Text);
}

}

public void BindGridview(string eachGroup,string strDate)
{
SqlConnection con = new SqlConnection(strCS);
string strQury = "select * from tblAuctionGroup where [Group]='" + eachGroup + "' and Date='" + strDate + "'";
SqlDataAdapter da = new SqlDataAdapter(strQury, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}

Thanks

Thanks
tvn



Responses

Posted by: Jhdotnet1 on: 6/27/2010 [Member] Starter

Up
0
Down
Hi,

You are binding a GridView using foreach. eveytime a particular row will be binding to the GridView. Row will be getting overwriting to GridView.

i.e GridView is getting clear and binding new row to GridView based on Foreach rows count. so fianlly Gridview bind to last row only..


Just Make one common DataSet and assign each row to this DataSet using BindGridview. Bind this common dataset to Gridview after Complete the foreach statement.

You will get clear output..

Declare common dataset to page level
DataSet dsCommon = new DataSet();


protected void btnClick_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strCS);
string strQury = "select [Group] from tblCust1 where CustID='"+ddlCustID.SelectedItem.ToString()+"'";
SqlDataAdapter da = new SqlDataAdapter(strQury, con);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
strGroup = row["Group"].ToString();
BindGridview(strGroup,txtDate.Text);
}

GridView1.DataSource = dsCommon .Tables[0];
GridView1.DataBind();
}

public void BindGridview(string eachGroup,string strDate)
{
SqlConnection con = new SqlConnection(strCS);
string strQury = "select * from tblAuctionGroup where [Group]='" + eachGroup + "' and Date='" + strDate + "'";
SqlDataAdapter da = new SqlDataAdapter(strQury, con);
DataSet ds = new DataSet();
da.Fill(ds);

//It will be called first foreach index
if(dsCommon != null)
dsCommon= ds.Table[0].Clone();

dsCommon.Tables[0].Rows.Add(ds.Tables[0].Rows[0]);

}
}

Can you try this sample and i didn't execute this sample program & didn't handle any exception

Thanks


Vanchi050585, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response