Gridview仅显示一个列数据 [英] Gridview showing only one column data

查看:91
本文介绍了Gridview仅显示一个列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从下拉列表中选择任何表名,我有下拉列表(tablenames),列表框中显示的列名(所选的dropdwon表中的所有coumn名称,当我多选列名(listbox)时我想显示所有数据(选中)列表框中的列)网格视图中有多列。



错误

多个选择gridview只显示一列(绑定)。帮我在网格视图中绑定多个列。



我尝试过:



I have dropdown(tablenames) if i select any table name from dropdown, the column names displayed in listbox(all coumn name from selected dropdwon table,when i multiple select column name(listbox) i want to disply all data(selected columns in listbox) in grid view with multiple columns.

error
while multiple select gridview displays only one column(bind).help me to bind multiple columns in grid view.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


namespace entity
{
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*  if (DropDownList1.SelectedValue == DropDownList1.SelectedItem.Text)
              {
                  string query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME="+ "DropDownList1.SelectedItem.Text+" ;
                  DataTable dt = _Default.ExecuteQuery(query);
                  ListBox1.Items.Clear();
                  ListBox1.DataSource = dt;
                  ListBox1.DataBind();

              }*/
            string query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='" + DropDownList1.SelectedItem.Text + "'";



            using (SqlCommand cmd = new SqlCommand(query))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {

                        sda.Fill(ds);
                        ListBox1.DataMember = "COLUMN_NAME";
                        ListBox1.DataValueField = "COLUMN_NAME";
                        ListBox1.DataSource = ds.Tables[0];
                        ListBox1.DataBind();

                    }

                }
            }
        }







        protected void Button2_Click(object sender, EventArgs e)
        {

            //ListBox1_SelectedIndexChanged(sender, e);
            if (ListBox1.Items.Count > 0)
            {

                DataTable dt = new DataTable();

                DataSet ds = new DataSet();

                for (int i = 0; i < ListBox1.Items.Count; i++)
                {
                    if (ListBox1.Items[i].Selected)
                    {
                        string Listbfrom = ListBox1.Items[i].Text;





                        con.Open();

                        string str = "SELECT  " + Listbfrom + "  FROM  " + DropDownList1.SelectedItem.Text;

                        SqlCommand com = new SqlCommand(str, con);

                        DataSet dsBooking = new DataSet();

                        SqlDataAdapter dap = new SqlDataAdapter(com);

                        dap.Fill(dsBooking);

                        con.Close();


                        

                            GridView1.DataSource = dsBooking;

                            GridView1.DataBind();

                        

                    }

                }

            }



        }

       /* protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ListBox1.Items.Count > 0)
            {

                DataTable dt = new DataTable();

                DataSet ds = new DataSet();

                for (int i = 0; i < ListBox1.Items.Count; i++)
                {
                    if (ListBox1.Items[i].Selected)
                    {
                        string Listbfrom = ListBox1.Items[i].Text;





                        con.Open();

                        string str = "SELECT  " + Listbfrom + "  FROM  " + DropDownList1.SelectedItem.Text;

                        SqlCommand com = new SqlCommand(str, con);

                        DataSet dsBooking = new DataSet();

                        SqlDataAdapter dap = new SqlDataAdapter(com);

                        dap.Fill(dsBooking);

                        con.Close();


                        {

                            GridView1.DataSource = dsBooking;

                            GridView1.DataBind();

                        }

                    }

                }

            }

        }
*/
    }
}

推荐答案

正如Vino Jangle指出你正在进行查询对于循环中的列 - 您实际需要做的是获取选择到逗号分隔列表中的列列表并使用它。



你应该也可以使用参数化查询 - 永远不要连接字符串来创建sql命令。



试试这个:
As Vino Jangle has pointed out you are doing the query for the column within the loop - what you actually need to do is get the list of columns that are selected into a comma-separated list and use that.

You should also be using Parameterized queries - never, ever concatenate strings to create sql commands.

Try this instead:
protected void Button2_Click(object sender, EventArgs e)
{

    if (ListBox1.Items.Count > 0)
    {
        List<string> listbfrom = new List<string>();

        for (int i = 0; i < ListBox1.Items.Count; i++)
            if (ListBox1.Items[i].Selected)
                listbfrom.Add(ListBox1.Items[i].Text);

        string csv = string.Join(",", listbfrom);

        con.Open();

        string str = "SELECT @listbfrom FROM  @tabfrom";

        using (SqlCommand com = new SqlCommand(str, con))
        {
            com.Parameters.AddWithValue("@listbfrom", csv);
            com.Parameters.AddWithValue("@tabfrom", DropDownList1.SelectedItem.Text);
            DataSet dsBooking = new DataSet();
            SqlDataAdapter dap = new SqlDataAdapter(com);

            dap.Fill(dsBooking);
            con.Close();
        }

        GridView1.DataSource = dsBooking;
        GridView1.DataBind();
    }
}



如果您有其他问题,请不要包含注释掉的代码或空方法。只发布您遇到问题的代码。



上述解决方案仅适用于.NET 4及更高版本。对于.NET 3.5,替换


If you have further questions please do not include commented out code or empty methods. Only post the code you are having a problem with.

The solution above only works for .NET 4 and above. For .NET 3.5 replacing

string csv = string.Join(",", listbfrom);

with

string csv = string.Join(",", listbfrom.ToArray());


这篇关于Gridview仅显示一个列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆