public static System.Data.DataTable ToDataTable <T>(this IEnumerable <T> collection) { System.Data.DataTable dt = new System.Data.DataTable(); Type t = typeof(T); System.Reflection.PropertyInfo[] pia = t.GetProperties(); //Create the columns in the DataTable foreach (System.Reflection.PropertyInfo pi in pia) { if ((pi.PropertyType.IsGenericType)) { Type typeOfColumn = pi.PropertyType.GetGenericArguments()[0]; dt.Columns.Add(pi.Name, typeOfColumn); } else { dt.Columns.Add(pi.Name, pi.PropertyType); } } //Populate the table foreach (T item in collection) { System.Data.DataRow dr = dt.NewRow(); dr.BeginEdit(); foreach (System.Reflection.PropertyInfo pi in pia) { dr[pi.Name] = pi.GetValue(item, null); } dr.EndEdit(); dt.Rows.Add(dr); } return(dt); }
private void cboSelectedIndex(object sender, System.EventArgs e) { System.Data.DataRow dr = _contacto.ListaClientes.Rows.Find(grdCliente[grdCliente.CurrentRowIndex, 0]); if (dr != null) { dr.BeginEdit(); dr["TipoContacto"] = ((ComboBox)sender).SelectedValue; dr["TipoContactoDescripcion"] = ((ComboBox)sender).Text; dr.EndEdit(); } }
private void cboSelectedIndex(object sender, System.EventArgs e) { System.Data.DataRow dr = _asignacion.ListaCobradores.Rows.Find(grdListaCobradores[grdListaCobradores.CurrentRowIndex, 0]); if ((dr != null) && (Convert.ToInt32(dr["EmpleadoResguardo"]) != Convert.ToInt32(((ComboBox)sender).SelectedValue))) { dr.BeginEdit(); dr["EmpleadoResguardo"] = ((ComboBox)sender).SelectedValue; dr["EmpleadoResguardoNombre"] = ((ComboBox)sender).Text; dr.EndEdit(); } }
public virtual void ImportXls(System.Data.DataTable dt) { int rowcnt = 1; string keyfield = dt.Columns[0].Caption; Object[,] data = readCell(1, ++rowcnt, dt.Columns.Count); string tmp = ""; while (data[1, 1] != null) { tmp += data[1, 1].ToString() + "{}"; System.Data.DataRow[] dr = dt.Select(keyfield + "='" + data[1, 1].ToString() + "'"); if (dr != null && dr.Length > 0) { dr[0].BeginEdit(); for (int i = 0; i < data.GetUpperBound(1); i++) { if (data[1, i + 1] != null) { dr[0][i] = data[1, i + 1]; } } dr[0].EndEdit(); } else { System.Data.DataRow r = dt.NewRow(); r.BeginEdit(); for (int i = 0; i < data.GetUpperBound(1); i++) { if (data[1, i + 1] != null) { r[i] = data[1, i + 1]; } } r.EndEdit(); dt.Rows.Add(r); } data = readCell(1, ++rowcnt, dt.Columns.Count); } }
private void btnSave_Click(object sender, RoutedEventArgs e) { if (action == ActionState.New) { try { System.Data.DataRow newRow = phoneNumbersDataSet.PhoneNumbers.NewRow(); newRow.BeginEdit(); newRow["Phonenum"] = txtPhoneNumber.Text.Trim(); newRow["Subscriber"] = txtSubscriber.Text.Trim(); newRow["Contract_Value"] = Int32.Parse(txtContractValue.Text.Trim()); newRow["Contract_Date"] = DateTime.Parse(txtContractDate.Text.Trim()); newRow.EndEdit(); phoneNumbersDataSet.PhoneNumbers.Rows.Add(newRow); tblPhoneNumbersAdapter.Update(phoneNumbersDataSet.PhoneNumbers); phoneNumbersDataSet.AcceptChanges(); } catch (System.Data.DataException ex) { phoneNumbersDataSet.RejectChanges(); MessageBox.Show(ex.Message); } btnNew.IsEnabled = true; btnEdit.IsEnabled = true; btnSave.IsEnabled = false; btnCancel.IsEnabled = false; lstPhones.IsEnabled = true; btnPrevious.IsEnabled = true; btnNext.IsEnabled = true; txtPhoneNumber.IsEnabled = false; txtSubscriber.IsEnabled = false; txtContractValue.IsEnabled = false; txtContractDate.IsEnabled = false; } else if (action == ActionState.Edit) { try { System.Data.DataRow editRow = phoneNumbersDataSet.PhoneNumbers.Rows[lstPhones.SelectedIndex]; editRow.BeginEdit(); editRow["Phonenum"] = txtPhoneNumber.Text.Trim(); editRow["Subscriber"] = txtSubscriber.Text.Trim(); editRow["Contract_Value"] = Int32.Parse(txtContractValue.Text.Trim()); editRow["Contract_Date"] = DateTime.Parse(txtContractDate.Text.Trim()); editRow.EndEdit(); tblPhoneNumbersAdapter.Update(phoneNumbersDataSet.PhoneNumbers); phoneNumbersDataSet.AcceptChanges(); } catch (System.Data.DataException ex) { phoneNumbersDataSet.RejectChanges(); MessageBox.Show(ex.Message); } btnNew.IsEnabled = true; btnEdit.IsEnabled = true; btnDelete.IsEnabled = true; btnSave.IsEnabled = false; btnCancel.IsEnabled = false; lstPhones.IsEnabled = true; btnPrevious.IsEnabled = true; btnNext.IsEnabled = true; txtPhoneNumber.IsEnabled = false; txtSubscriber.IsEnabled = false; txtContractValue.IsEnabled = false; txtContractDate.IsEnabled = false; txtPhoneNumber.SetBinding(TextBox.TextProperty, txtPhoneNumberBinding); txtSubscriber.SetBinding(TextBox.TextProperty, txtSubscriberBinding); } else if (action == ActionState.Delete) { try { System.Data.DataRow deleterow = phoneNumbersDataSet.PhoneNumbers.Rows[lstPhones.SelectedIndex]; deleterow.Delete(); tblPhoneNumbersAdapter.Update(phoneNumbersDataSet.PhoneNumbers); phoneNumbersDataSet.AcceptChanges(); } catch (System.Data.DataException ex) { phoneNumbersDataSet.RejectChanges(); MessageBox.Show(ex.Message); MessageBox.Show(ex.Message); } btnNew.IsEnabled = true; btnEdit.IsEnabled = true; btnDelete.IsEnabled = true; btnSave.IsEnabled = false; btnCancel.IsEnabled = false; lstPhones.IsEnabled = true; btnPrevious.IsEnabled = true; btnNext.IsEnabled = true; txtPhoneNumber.IsEnabled = false; txtSubscriber.IsEnabled = false; txtPhoneNumber.SetBinding(TextBox.TextProperty, txtPhoneNumberBinding); txtSubscriber.SetBinding(TextBox.TextProperty, txtSubscriberBinding); txtContractValue.SetBinding(TextBox.TextProperty, txtContractValueBinding); txtContractDate.SetBinding(TextBox.TextProperty, txtContractDateBinding); } }