private void btnOK_Click(object sender, EventArgs e) { if (this.txtBxNick.Text.Length == 0 || this.txtBxPhoneNum.Text.Length == 0) { return; } var user = User.UserExists(this.txtBxPhoneNum.Text.Trim(), this.txtBxNick.Text.Trim()); this.Tag = user; this.DialogResult = DialogResult.OK; this.Close(); }
private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { string filePath = openFileDialog1.FileName; string extension = Path.GetExtension(filePath); /* string header = rbHeaderYes.Checked ? "YES" : "NO";*/ string header = "NO"; string conStr, sheetName; conStr = string.Empty; switch (extension) { case ".xls": //Excel 97-03 conStr = string.Format(Excel03ConString, filePath, header); break; case ".xlsx": //Excel 07 conStr = string.Format(Excel07ConString, filePath, header); break; } //Get the name of the First Sheet. using (OleDbConnection con = new OleDbConnection(conStr)) { using (OleDbCommand cmd = new OleDbCommand()) { cmd.Connection = con; con.Open(); DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString(); con.Close(); } } //Read Data from the First Sheet. using (OleDbConnection con = new OleDbConnection(conStr)) { using (OleDbCommand cmd = new OleDbCommand()) { using (OleDbDataAdapter oda = new OleDbDataAdapter()) { DataTable dt = new DataTable(); cmd.CommandText = "SELECT * From [" + sheetName + "]"; cmd.Connection = con; con.Open(); oda.SelectCommand = cmd; oda.Fill(dt); con.Close(); /* List<KeyValuePair<String, String>> test = new List<KeyValuePair<string, string>>();*/ foreach (DataRow dr in dt.Rows) { test.Add(new KeyValuePair <string, string>(dr[0].ToString(), (dr[1].ToString()))); } foreach (var item in test) { var tmpUser = User.UserExists("57" + item.Value, item.Key); this.userList.Add(tmpUser.PhoneNumber, tmpUser); var tmpListUser = new ListViewItem(tmpUser.UserName); tmpListUser.Tag = tmpUser; this.listViewContacts.Items.Add(tmpListUser); } } } } }