简体   繁体   中英

Question about displaying sql server data in a grid

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 SqlConnection(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.

我弄清楚了,我只是写了自己的查询来显示某些列,而不是自动显示所有列。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM