List view
Hi frnds plz help me, I am displaying values fro database to listview through code by'listvw.datasource=ds', but i didnt get the values to be displayed in list view,
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Glenn Sanchez30 reputationPosted May 17, 2012, 9:36 PM EDT
Kunal VaishyaRank #990 · 2K reputationPosted May 17, 2012, 9:38 PM EDT
If
Ds is DataSet Then
try this
listvw.datasource=ds.Table[0];
listvw.dataBind();
Glenn Sanchez30 reputationPosted May 17, 2012, 9:45 PM EDT
EhteshamRank #3,979 · 374 reputationPosted May 17, 2012, 9:36 PM EDT
listvw.datasource=ds.tables[0];
listvw.dataBind();
anil john153 reputationPosted May 17, 2012, 9:40 PM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted May 18, 2012, 12:06 AM EDT
Please mention it is a web or windows application.If it is a windows application then below is the code.
Try this...
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Listview_display
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
OleDbDataAdapter oledbda;
string str;
DataSet ds;
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listView1.Columns.Add("ID", 70, HorizontalAlignment.Center);
listView1.Columns.Add("GRADE", 70, HorizontalAlignment.Center);
listView1.View = View.Details;
listView1.GridLines = true;
listView1.BackColor = Color.Aqua;
listView1.ForeColor = Color.Blue;
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select * from test Query";
com = new OleDbCommand(str, con);
oledbda = new OleDbDataAdapter(com);
ds = new DataSet();
oledbda.Fill(ds, "test Query");
con.Close();
dt = ds.Tables["test Query"];
int i;
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
listView1.Items.Add(dt.Rows[i].ItemArray[0].ToString());
listView1.Items[i].SubItems.Add(dt.Rows[i].ItemArray[1].ToString());
}
}
}
}
Thanks
If this post helps you mark it as answer
Satyapriya NayakRank #47 · 31.5K reputationPosted May 18, 2012, 3:30 AM EDT
For web application below code
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ListView_in_Asp.net._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace ListView_in_Asp.net
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
ListView1.DataMember = "employee";
ListView1.DataSource = ds;
ListView1.DataBind();
con.Close();
}
}
}
Thanks
If this post helps you mark it as answer