开发者

Question about displaying sql server data in a grid

开发者 https://www.devze.com 2023-02-11 01:39 出处:网络
I am pulling data from a sql server and putting it into a grid using c#. When the data displays on the grid, it is showing up as the guid rather than the actual name. How do I get the name to show and

I am pulling data from a sql server and putting it into a grid using c#. When the data displays on the grid, it is showing up as the guid rather than the actual name. How do I get the name to show and not the uniqe identifier. Any ideas? Thanks.

Here is some of the code:

public InventoryWindow()
    {
        InitializeComponent();

        if (dgDataView != null)
        {
            SqlConnection con = new SqlConnection(connString);
            SqlDataAdapter adpt = new SqlDataAdapter("select * from Item", con);
            DataSet ds = new DataSet();
            adpt.Fill(ds, "Item");
            dgDataView.DataContext = ds;
            //dgDataView.DataMember = "Item";
            showdata();
        }

    }

    private void showdata()
    {
        String connString = "server=server;database=database;user=user;password=password";

        SqlConnection con = new SqlConnect开发者_开发知识库ion(connString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Item", con);
        SqlDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(dr);
        dgDataView.DataContext = dt;
        con.Close();
    }


You are using select * from Item and therefore returning all columns. You could just specify the columns you want in the Grid, in the order you want them. The grid by default has autocolumn generation on.

You can also specify the columns you want and what fields they map to using the columns DataMember values.


I figured this out, I just wrote my own query to display certain columns instead of automatically showing all of them.

0

精彩评论

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