简体   繁体   English

关于在网格中显示SQL Server数据的问题

[英]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#. 我正在从sql服务器中提取数据,并使用c#将其放入网格中。 When the data displays on the grid, it is showing up as the guid rather than the actual name. 当数据显示在网格上时,它将显示为guid而不是实际名称。 How do I get the name to show and not the uniqe identifier. 如何获取要显示的名称而不是uniqe标识符。 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. 您正在使用select * from Item ,因此返回所有列。 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. 您还可以使用列DataMember值指定所需的列以及它们映射到的字段。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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