Title: Question about displaying sql server data in a grid
Tags: c#;tsql
Question: 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();
Comment: Can you post the sql query thats being executed together with the column definitions? - also a sample of the code would be good

Comment: please elaborate a little more what are you doing, components using, page code and code behind, etc.

Here is the accepted answer: I figured this out, I just wrote my own query to display certain columns instead of automatically showing all of them.

Here is another answer: 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.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2011:00:02/16/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><tsql>', 'Title': 'Question about displaying sql server data in a grid', 'ViewCount': 533, 'id': 5018189, 'user_AboutMe': '', 'user_AccountId': 684708.0, 'user_DisplayName': 'Joe Smith', 'user_Reputation': 205.0, 'user_Views': 46.0, 'user_id': 541046.0}}