Listview mit Daten fuellen beim Formular oeffnen
-
Hallo,
ich habe noch nicht all zu viel Ahnung von C# und bin nach kurzer Zeit auf ein Problem gestossen welches ich nicht im Stande bin zu loesen. Ich moechte gerne das wenn mein Formular gestartet wird die Verbindung mit dem SQL Server aufgebaut wird und die DS angezeigt werden. In einem Beispiel aus dem ich meinen Code teilweise uebernommen habe funktioniert es wenn man einen Button drueckt. Daher habe ich den Code unter Startform_load plaziert aber nichts passiert.
Ich hoffe ihr koennt mir helfen.
[cs]namespace Lamis
{
using System.Data.SqlClient;
/// <summary>
/// Summary description for Contact.
/// </summary>
public class frm_Contact : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu Menue_CRM;
private System.Windows.Forms.MenuItem MCRM_File;
private System.Windows.Forms.MenuItem MCRM_Close;
private System.Windows.Forms.ListView list_company;
private System.Data.SqlClient.SqlConnection sql_list_com;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;public frm_Contact()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();//
// TODO: Add any constructor code after InitializeComponent call
//
}/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Menue_CRM = new System.Windows.Forms.MainMenu();
this.MCRM_File = new System.Windows.Forms.MenuItem();
this.MCRM_Close = new System.Windows.Forms.MenuItem();
this.list_company = new System.Windows.Forms.ListView();
this.sql_list_com = new System.Data.SqlClient.SqlConnection();
this.SuspendLayout();
//
// Menue_CRM
//
this.Menue_CRM.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.MCRM_File});
//
// MCRM_File
//
this.MCRM_File.Index = 0;
this.MCRM_File.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.MCRM_Close});
this.MCRM_File.Text = "File";
//
// MCRM_Close
//
this.MCRM_Close.Index = 0;
this.MCRM_Close.Text = "Close";
this.MCRM_Close.Click += new System.EventHandler(this.MCRM_Close_Click);
//
// list_company
//
this.list_company.Location = new System.Drawing.Point(112, 24);
this.list_company.Name = "list_company";
this.list_company.Size = new System.Drawing.Size(480, 104);
this.list_company.TabIndex = 0;
this.list_company.SelectedIndexChanged += new System.EventHandler(this.list_company_SelectedIndexChanged);
//
// sql_list_com
//
this.sql_list_com.ConnectionString = "Workstation ID=Tim;packet size=4096;integrated security=SSPI;data source=\"SERVER-" +
"POZNAN\";persist security info=False;initial catalog=Lamis";
//
// frm_Contact
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(736, 329);
this.Controls.Add(this.list_company);
this.Menu = this.Menue_CRM;
this.Name = "frm_Contact";
this.Text = "LaMIS CRM";
this.ResumeLayout(false);}
#endregionstatic void Main()
{
Application.Run(new frm_Contact());
}const string CONNSTR = "Workstation ID=Tim;packet size=4096;integrated security=SSPI;data source=\"SERVER-" +
"POZNAN\";persist security info=False;initial catalog=Lamis";private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{}
private void MCRM_Close_Click (object sender, System.EventArgs e)
{
Close();
}private void customer_Click(object sender, System.EventArgs e)
{}
private void list_company_SelectedIndexChanged(object sender, System.EventArgs e)
{}
**private void StartForm_Load(object sender, System.EventArgs e)
{
const string sqlstr = "select com_company_id, com_name, com_street, com_postcode, com_city FROM view_customer_company";
SqlConnection conn = new SqlConnection(CONNSTR);
SqlCommand cmd = new SqlCommand(sqlstr, conn);
SqlDataReader dr;
ListViewItem lvItem;list_company.View = View.Details;
list_company.AllowColumnReorder = true;
list_company.FullRowSelect=true;
list_company.Items.Clear();
list_company.Columns.Add("com_company_id",80, HorizontalAlignment.Left);
list_company.Columns.Add("com_name", 200, HorizontalAlignment.Left);
list_company.Columns.Add("com_street",150, HorizontalAlignment.Left);
list_company.Columns.Add("com_postcode",80, HorizontalAlignment.Left);
list_company.Columns.Add("com_city",150, HorizontalAlignment.Left);
try
{
cmd.Connection.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); // DataReader wird erzeugt
while (dr.Read())
{
lvItem = new ListViewItem(dr["com_company_id"].ToString());
lvItem.SubItems.Add(dr["com_name"].ToString());
lvItem.SubItems.Add(dr["com_street"].ToString());
lvItem.SubItems.Add(dr["com_postcode"].ToString());
lvItem.SubItems.Add(dr["com_city"].ToString());
list_company.Items.Add(lvItem); // Items zum ListView hinzufügen
}
}
catch (Exception ex )
{
MessageBox.Show(ex.Message);
}
cmd.Connection.Close();**}
}}
[/cs]
-
Bist Du sicher, dass Startform_load das FormLoad Ereignis Deiner Form ist?
Das FormLoad Ereignis Deiner Form kannst Du behandeln, indem Du einen Doppelklick auf die Form machst.
-
Vielen Dank,
es funktioniert!!!!
Thanx