当我使用csharp单击组合框时,如何在datagridview中添加列 [英] how to add column in datagridview when i click the combobox using csharp
问题描述
在设计中我有一个组合框。在那里有组合框项目。
当我选择项目时组合框,要在数据网格中添加的项目按行排序。
我该怎么办。
例如组合框项目如下
组合框Rs
GS
VB
当我在组合框中选择VB时,Gs表示要在行数据视图中添加项目。
输出如下;
VB GS
我该怎么办。请帮助我。
in the design i have one combo box.in that combo box items are there.
when i select items in the combo box that items to be added in data grid ivew by rows.
how can i do.
for example in the combobox items are as follows
combobox Rs
GS
VB
when i select the VB,Gs in combobox that items to be added in datagridview by rows.
Ouput as follows;
VB GS
how can i do.please help me.
推荐答案
重写为
rewrite as
protected void combobox_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = new DataTable;
dt.Columns.Add("ComboBoxItems");
// add combox selected item to datatable object
dt.Rows.Add(new object[] { combox.SelectedItem.Text });
DataGridView.DataSource = dt;
}
试试这个...
try this...
public void LoadGridView()
{
Datatable dt = (Datatale)ViewState("CurrentTable");
GridView.DataSource = dt;
GridView.DataBind();
}
Combobox selectedindexchanged event
Combobox selectedindexchanged event
protected void combobox_SelectedIndexChanged(object sender, EventArgs e)
{
Datatable dt = (Datatale)ViewState("CurrentTable");
if(ViewState("CurrentTable")!= null)
{
dt = (Datatale)ViewState("CurrentTable");
}
else
{
dt = new Datattable;
}
dt.Colums.Add("ComboBoxItems");
// add combox selected item to datatable object
dt.Rows.Add(new object[] { combox.SelectedItem.Text });
ViewState("CurrentTable") = dt;
// call LoadGridView Funcation
LoadGridView();
}
希望这会对你有帮助..
hope this will help you..
这篇关于当我使用csharp单击组合框时,如何在datagridview中添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!