请问如何实现datagrid的列的添加和删除?

darthhiker 2003-10-17 07:45:50
需求是这样的,一个datagrid,有3个列是固定显示的。其他的列按查询条件来决定是添加还是删除。比如,选择一个条件,datagird中就显示相应的列,不选的话就不显示。
也考虑用隐藏列的方法,但不知道该如何实现,望高人指点,谢谢。
...全文
36 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
darthhiker 2003-10-18
  • 打赏
  • 举报
回复
谢谢各位支持,等我尝试成功后一定给分
OOSnoopy 2003-10-17
  • 打赏
  • 举报
回复
一個例子,代碼我沒有貼完,只有主要代碼。請看行不行:


private void Form1_Load(object sender, System.EventArgs e)
{
DataColumn dcID = new DataColumn("ID", System.Type.GetType("System.Int32"));
dcID.AllowDBNull = false;
this.dt.Columns.Add(dcID);

DataColumn dcName = new DataColumn("Name", System.Type.GetType("System.String"));
this.dt.Columns.Add(dcName);

DataColumn dcSex = new DataColumn("Sex", System.Type.GetType("System.String"));
this.dt.Columns.Add(dcSex);

this.dt.PrimaryKey = new DataColumn[] {this.dt.Columns[0]};


this.dataGridTextBoxColumn1.MappingName = "ID";
this.dataGridTextBoxColumn2.MappingName = "Name";
this.dataGridTextBoxColumn3.MappingName = "Sex";
this.dataGridTableStyle1.MappingName = this.dt.TableName;
this.dataGrid1.DataSource = this.dt.DefaultView;
this.checkBox1.Checked = true;
}



private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{
if(this.checkBox1.Checked)
this.dataGridTextBoxColumn2.MappingName = "Name";
else
this.dataGridTextBoxColumn2.MappingName = "";
this.dataGrid1.DataSource = this.dt.DefaultView;
}
Stevetan81 2003-10-17
  • 打赏
  • 举报
回复
先在 datatable or dateview中加入目标列,在邦定不知道可不可以
hackking 2003-10-17
  • 打赏
  • 举报
回复
动态构造数据源,绑定到datagrid上即可
OOSnoopy 2003-10-17
  • 打赏
  • 举报
回复
這個好做啊,你把你 DataGrid中對應的DataGridTextBoxStyle或DataGridBoolStyel對象的MapingName屬性高為""就可以隱藏了,要顯示的時候又給它賦上值,然後重新運行一下DataGrid.DataSorce或DataGrid.SetDataBinding()。如果把Width高為0,顯示時有時表的列標題會有得復顯示的情況,我這里遇到過的。你試一下了。
rengang11 2003-10-17
  • 打赏
  • 举报
回复
public void setDataGridView(ref DataGrid grid,String proj_id)
{
//权限字典
String strSel = "select dict_code,code_name from dictionary_define where dict_division='001' ";
DataTable table= getDataTable(strSel);
if (table.Rows.Count>0)
{

grid.Columns.Clear();
// 设置GRID的删除列
ButtonColumn btnCol = new ButtonColumn();
btnCol.HeaderText ="删除";
btnCol.Text = "删除";
btnCol.CommandName ="Delete";
btnCol.ItemStyle.HorizontalAlign = HorizontalAlign.Center ;
btnCol.ButtonType = ButtonColumnType.LinkButton;
grid.Columns.Add(btnCol);
// 设置GRID的编辑列
EditCommandColumn edCol= new EditCommandColumn();
edCol.ButtonType =ButtonColumnType.LinkButton;
edCol.HeaderText ="编辑";
edCol.EditText ="编辑";
edCol.ItemStyle.HorizontalAlign = HorizontalAlign.Center ;
grid.Columns.Add(edCol);
// 设置GRID的项目列
BoundColumn boundCol = new BoundColumn();
boundCol.Visible = false;
boundCol.DataField = "proj_id";
boundCol.ItemStyle.HorizontalAlign = HorizontalAlign.Center ;
grid.Columns.Add(boundCol);
// 设置GRID的用户ID列
boundCol = new BoundColumn();
boundCol.Visible = false;
boundCol.DataField = "user_id";
boundCol.ItemStyle.HorizontalAlign = HorizontalAlign.Center ;
grid.Columns.Add(boundCol);
// 设置GRID的职务ID列
boundCol = new BoundColumn();
boundCol.Visible = false;
boundCol.DataField = "role_id";
boundCol.ItemStyle.HorizontalAlign = HorizontalAlign.Center ;
grid.Columns.Add(boundCol);
// 设置GRID的用户名列
boundCol = new BoundColumn();
boundCol.DataField = "user_name";
boundCol.HeaderText = "用户名";
boundCol.ItemStyle.HorizontalAlign = HorizontalAlign.Center ;
grid.Columns.Add(boundCol);
// 设置GRID的职务名列
boundCol = new BoundColumn();
boundCol.DataField = "code_name";
boundCol.HeaderText = "职务";
grid.Columns.Add(boundCol);
//创建显示的数据表
DataTable tableView = new DataTable();
//设置数据表的列数
tableView.Columns.Add("proj_id",typeof(String));
tableView.Columns.Add("user_id",typeof(String));
tableView.Columns.Add("role_id",typeof(String));
tableView.Columns.Add("user_name",typeof(String));
tableView.Columns.Add("code_name",typeof(String));
//根据数据设置其余的列
for (int i=0 ;i<table.Rows.Count;i++)
{
boundCol = new BoundColumn();
boundCol.DataField = table.Rows[i]["dict_code"].ToString();
boundCol.HeaderText = table.Rows[i]["code_name"].ToString();
boundCol.ItemStyle.HorizontalAlign = HorizontalAlign.Center ;
grid.Columns.Add(boundCol);
tableView.Columns.Add(table.Rows[i]["dict_code"].ToString());
}

//显示数据的准备
strSel = "select project_user_inf.proj_id,project_user_inf.user_id,project_user_inf.role_id,user_name,acl_id,code_name from project_user_inf left join acl_assign on "
+" project_user_inf.proj_id=acl_assign.proj_id and project_user_inf.user_id=acl_assign.user_id inner join user_info on "
+" project_user_inf.user_id=user_info.user_id inner join dictionary_define on project_user_inf.role_id=dictionary_define.dict_code "
+" where dictionary_define.dict_division='022' and "
+" project_user_inf.proj_id='"+proj_id+"' order by project_user_inf.user_id asc ,project_user_inf.role_id desc ";
table = getDataTable(strSel);
String user_id ="";
int intColCount = tableView.Columns.Count;
DataRow row = null;
for (int i=0;i<table.Rows.Count;i++)
{
DataRow temp = table.Rows[i];

if (!user_id.Equals(temp["user_id"]))
{
if (row != null) tableView.Rows.Add(row);
row = tableView.NewRow();
for (int j=0;j<intColCount;j++)
{
row[j]= "X";
}
}
user_id = temp["user_id"].ToString();
row["user_name"] = temp["user_name"];
row["proj_id"] = proj_id;
row["user_id"] = user_id;
row["role_id"] = temp["role_id"];
row["code_name"]= temp["code_name"];
if (temp["acl_id"].ToString().Equals("")) continue;
row[temp["acl_id"].ToString()] ="O";


}
if (row != null) tableView.Rows.Add(row);
grid.DataSource =tableView.DefaultView;
grid.DataBind();
}
}


上面这段代码就是我们已经实现的动态显示数据列,非常不好做,希望你能看懂,
记得给分哦:)
kuangsha007 2003-10-17
  • 打赏
  • 举报
回复
在web窗体里面
可以动态的来设置列的visible属性。
在windows窗体里面
可以设定列的width=0;
win911 2003-10-17
  • 打赏
  • 举报
回复
隐藏在现有 Windows 窗体 DataGrid 控件中显示的 DataTable 对象的“X”列。
示例
Sub HideColumnOfDataSet()
Dim points As New System.Data.DataTable("Points")
points.Columns.Add(New DataColumn("X", GetType(Integer)))
points.Columns.Add(New DataColumn("Y", GetType(Integer)))
points.Rows.Add(New Object() {1, 2})
points.Rows.Add(New Object() {3, 5})
DataGrid1.DataSource = points

Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "Points"
DataGrid1.TableStyles.Add(tableStyle)
DataGrid1.TableStyles("Points").GridColumnStyles("X").Width = 0
End Sub

以上是vb.net的示例.
你也可以在msdn上查找GridColumnStyles,DataGridTableStyle来找到相关内容
haoliqi 2003-10-17
  • 打赏
  • 举报
回复
不用的列就用代码让它隐藏,也能实现
jiezhi 2003-10-17
  • 打赏
  • 举报
回复
动态的给数据源不就行了.

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧