如何将数据显示到列表中从数据库查看。 [英] How to show data into list View from database.
问题描述
Hi Team,
我正在制作一个简单的基于Windows的C#应用程序
我有一个表EMP包含一些数据,如
姓名教育电子邮件电话
John BE john@gmail.com 1234
我有一个ListView控件和文本框用于搜索目的。
当我在文本框控件中输入John然后单击搜索按钮后....我想将John的数据从EMP表显示到listview控件中。
请帮助我。
Hi Team,
I am making one simple Windows based C# application
I have one table "EMP" which contains Some data like-
Name Education Email Phone
John BE john@gmail.com 1234
I have One ListView control and Textbox for search purpose.
when I enter John in textbox control then after clicking search button ....I want to show data for John from "EMP" table into listview control.
Please help me.
推荐答案
HI使用此示例代码完成您的任务..
note :数据是硬编码的,用于理解目的..你应该使用你的sql数据源..
HI use this sample code for your task..
note: data is hard coded for understanding purpose..you should use your sql datasource..
protected void btnSearch_Click(object sender, EventArgs e)
{
string name = txtbox.Text;
// use this name to get the data from database.
// and assign it to the listview.. as below..
}
listView1.View = View.Details;
listView1.Columns.Add("Name",100);
listView1.Columns.Add("Education",100);
listView1.Columns.Add("Email",100);
listView1.Columns.Add("Phone", 100);
listView1.Items.Clear();
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Education", typeof(string));
dt.Columns.Add("Email", typeof(string));
dt.Columns.Add("Phone", typeof(string));
dt.Rows.Add("karthik", "B.E CSE", "karthik@xyz.com", "98800");
dt.Rows.Add("parthip", "B.S Mech", "parthip@xyz.com", "98800");
foreach (DataRow row in dt.Rows)
{
ListViewItem li = new ListViewItem(row.ItemArray.Select(k => k + "").ToArray());
listView1.Items.Add(li);
}
这篇关于如何将数据显示到列表中从数据库查看。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!