Reputation: 243
I am using GridView and trying to bind data to this GridView, but I am facing unusual problem, here is my code :
Method to bind GridView :
private void BindDataSource()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
string queryString6 = "";
string items = "";
if (lb_add_col.Items.Count > 0)
{
foreach (ListItem listItem in lb_add_col.Items)
{
if (listItem.Selected == true)
{
items += listItem.Text + ",";
}
}
}
items = items.TrimEnd(',');
queryString6 = "SELECT "+items+" from " +ddl_tables.Items[0].Text;
System.Data.SqlClient.SqlCommand sqlCmd6 = new System.Data.SqlClient.SqlCommand(queryString6, con);
System.Data.SqlClient.SqlDataAdapter dataAdapter6 = new System.Data.SqlClient.SqlDataAdapter(sqlCmd6);
System.Data.DataSet dataSet6 = new System.Data.DataSet();
dataAdapter6.Fill(dataSet6);
string[] columns = items.Split(',');
for (int i = 0; i < dataSet6.Tables[0].Rows.Count; i++)
{
order.Add(new Orders(columns.Contains("id") ? Convert.ToInt32(dataSet6.Tables[0].Rows[i]["id"]) : 0, columns.Contains("name") ? dataSet6.Tables
[0].Rows[i]["name"].ToString() : "N/A", columns.Contains("month") ? dataSet6.Tables[0].Rows[i]["month"].ToString() : "N/A", columns.Contains("effiecency") ? Convert.ToInt32(dataSet6.Tables[0].Rows[i]
["effiecency"]) : 0, columns.Contains("latitude") ? Convert.ToInt32(dataSet6.Tables[0].Rows[i]["latitude"]) : 0, columns.Contains("longitude") ? Convert.ToInt32(dataSet6.Tables[0].Rows[i]
["longitude"]) : 0));
}
this.OrdersGrid.DataSource = order;
this.OrdersGrid.DataBind();
}
From above method order is showing the data but GridView is not binding.
Button Click event on which I am calling this method :
protected void btn_add_col_Click(object sender, EventArgs e)
{
Syncfusion.JavaScript.Models.Column sd;
foreach (ListItem listItem in lb_add_col.Items)
{
if (listItem.Selected == true)
{
sd = new Syncfusion.JavaScript.Models.Column();
if (listItem.Text == "id")
{
sd.IsPrimaryKey = true;
}
sd.Field = listItem.Text;
sd.HeaderText = listItem.Value;
sd.Width = 90;
sd.TextAlign = Syncfusion.JavaScript.TextAlign.Right;
this.OrdersGrid.Columns.Add(sd);
}
}
BindDataSource();
}
Upvotes: 0
Views: 1526
Reputation: 2058
If the order contains value after the loop has executed then the data source will be bind to the Grid. But the Grid does not contain the columns that you have defined in the button click, it return all columns in the grid data source. the Grid has only shown the column that you have define in the bindDataSource method,
Upvotes: 1
Reputation: 20638
if dataSet6.Tables[0].Rows.Count
is zero, the GridView will databind but there will be no data.
Upvotes: 0