我有这样的网格视图。我想只显示group_id和groups row。我怎样才能做到这一点?
这是我的代码:
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM groups WHERE user_id = '" + current_user + "'", conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
GroupGrid.DataSource = ds;
GroupGrid.DataBind();
答案 0 :(得分:2)
因此,当您使用SELECT * FROM groups
时,它将选择/获取表的所有列。因此,您只能选择/获取您希望显示的列。
而不是SELECT * FROM groups
您应该使用SELECT group_id, groups FROM groups
来解决问题
答案 1 :(得分:1)
即使您选择了表中的所有列
,也可以使用C#代码处理它 GroupGrid.DataSource = Nothing
GroupGrid.AutoGenerateColumns = False
GroupGrid.Columns(0).Name = "group_id"
GroupGrid.Columns(0).HeaderText = "Group ID"
GroupGrid.Columns(0).DataPropertyName = "group_id"
GroupGrid.Columns(1).Name = "groups"
GroupGrid.Columns(1).HeaderText = "Group"
GroupGrid.Columns(1).DataPropertyName = "groups"
GroupGrid.DataSource = ds;
GroupGrid.DataBind();