将列表框数据显示到Gridview中 [英] Displaying the Listbox data into Gridview
问题描述
我想将列表框数据显示到我试过的网格视图中,但我的下面的代码无效。
我的代码如下
Listbox1如下
Raj
suresh
Gopi
Vignesh
Ram
Listbox2如下
GM
IT
Acts
Rec
硬件
protected void Page_Load(object sender,EventArgs e)
{
Gridviewimage.Visible = true;
ListBox1.Items.Add(Raj);
ListBox1.Items.Add(suresh);
ListBox1.Items.Add(Gopi );
ListBox1.Items.Add(Vignesh);
ListBox1.Items.Add(Ram);
ListBox2.Items.Add(GM);
ListBox2.Items.Add(IT);
ListBox2.Items.Add( 使徒行传);
ListBox2.Items.Add(Rec);
ListBox2.Items.Add(硬件);
DataTable dt = new DataTable();
dt.Columns.Add(Name,typeof(string));
dt。 Columns.Add(Dpt,typeof(string));
if(ListBox1.Items.Count == ListBox2.Items.Count)
{
for(int i = 0;我< ListBox1.Items.Count; i ++)
{
dt.Rows.Add(ListBox1.Items [i] .Text,ListBox2.Items [i] .Text);
}
Gridviewimage.DataSource = dt;
Gridviewimage.DataBind();
}
} < br $> b $ b
当我在Gridview中执行上面的代码时,没有数据显示空的gridview。 br />
我上面的代码有什么问题请帮帮我。
问候,
Narasiman P.
I want to display the list box data into grid view i tried but my below code is not working.
My code as follows
Listbox1 as follows
Raj
suresh
Gopi
Vignesh
Ram
Listbox2 as follows
GM
IT
Acts
Rec
Hardware
protected void Page_Load(object sender, EventArgs e)
{
Gridviewimage.Visible = true;
ListBox1.Items.Add("Raj");
ListBox1.Items.Add("suresh");
ListBox1.Items.Add("Gopi");
ListBox1.Items.Add("Vignesh");
ListBox1.Items.Add("Ram");
ListBox2.Items.Add("GM");
ListBox2.Items.Add("IT");
ListBox2.Items.Add("Acts");
ListBox2.Items.Add("Rec");
ListBox2.Items.Add("Hardware");
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Dpt", typeof(string));
if (ListBox1.Items.Count == ListBox2.Items.Count)
{
for (int i = 0; i < ListBox1.Items.Count; i++)
{
dt.Rows.Add(ListBox1.Items[i].Text, ListBox2.Items[i].Text);
}
Gridviewimage.DataSource = dt;
Gridviewimage.DataBind();
}
}
When i execute my above code in Gridview no data is there it shows the empty gridview.
what is the problem in my above code please help me.
regards,
Narasiman P.
推荐答案
启用AutoGenerateColumns
Enable the AutoGenerateColumns
Gridviewimage.AutoGenerateColumns = true;
Gridviewimage.DataSource = dt;
Gridviewimage.DataBind();
这篇关于将列表框数据显示到Gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!