C# .NET - i want to show retreived records from if statement into the list view

Asked By Priyanka on 06-Dec-10 02:20 AM

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\hotel_management.mdb;Persist Security Info=True;Jet OLEDB:Database Password=randhawa");

string query = "Select * from checkintable where cust_name = '" + txt_customerName.Text + "'";

OleDbDataAdapter da = new OleDbDataAdapter(query, con);

DataSet ds = new DataSet();

da.Fill(ds);

if (ds.Tables[0].Rows.Count > 0)

{


i want to show retreived records from if statement into the list view columns... can you tell me how to do that?

}

else

{

MessageBox.Show("No such record found");

}

Sagar P replied to Priyanka on 06-Dec-10 02:25 AM
You can use this code to add items in LISTVIEW from dataset you filled;

DataTable dt = ds.Tables[0];
int fc = dt.Columns.Count;
lv.Columns.Clear();
lv.Items.Clear();
for (int i = 0; i < fc; i++)
{
  lv.Columns.Add("Column " + i.ToString());
}
foreach (DataRow row in dt.Rows)
{
  string[] subitems = new string[fc];
  object[] o = row.ItemArray;
  for (int i = 0; i < fc; i++)
  {
    subitems[i] = o[i].ToString();
  }
  ListViewItem item = new ListViewItem(subitems);
  lv.Items.Add(item);
}
lv.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);


Use this code inside IF
Jatin Prajapati replied to Priyanka on 06-Dec-10 02:27 AM
For this, put a DataGridView on the form and bind that DatgridView to your table in the if condition like this


OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\hotel_management.mdb;Persist Security Info=True;Jet OLEDB:Database Password=randhawa");
 
string query = "Select * from checkintable where cust_name = '" + txt_customerName.Text + "'";
 
OleDbDataAdapter da = new OleDbDataAdapter(query, con);
 
DataSet ds = new DataSet();
 
da.Fill(ds);
 
if (ds.Tables[0].Rows.Count > 0)
 
{
  dataGridView1.DataSource = ds.Tables[0];
}
 
else
 
{
 
MessageBox.Show("No such record found");
 
}