estoy usando un Datagrid(no datagridview ya que Windows mobile ce 5 no lo permite con compact framework) para mostrar los datos de un datatable, hasta aquí todo bien veo los datos y los filtro.
Ahora lo que necesito es que al hacer doble click en una fila llame a otro form en el que mediante un textbox pueda modificar los datos de esa fila para después actualizarlos en el fichero origen.
he visto dos métodos, mouseup usando hittest por compatibilidad con datagrid y celldoubleclick con currentcell.columnindex pero este ultimo creo que solo es compatible con gridview
aquí mi código hasta el momento:
public partial class Form1 : Form { public static string mstr;
public Form1() { InitializeComponent(); } DataTable tbl; DataRow dr; public DataTable ConvertToDataTable (string filePath, int numberOfColumns) { DataTable tbl = new DataTable(); tbl.Clear(); tbl.Columns.Add(new DataColumn("NOMBRE", typeof(string))); tbl.Columns.Add(new DataColumn("EDAD", typeof(string))); tbl.Columns.Add(new DataColumn("APELLIDO", typeof(string))); List<string> lines = new List<string>(); using (StreamReader reader = File.OpenText(filePath)) { while (!reader.EndOfStream) { lines.Add(reader.ReadLine()); } } foreach(string line in lines) { var cols = line.Split(';'); DataRow dr = tbl.NewRow(); for (int cIndex = 0; cIndex < 3; cIndex++) { dr[cIndex] = cols[cIndex]; } tbl.Rows.Add(dr); } return tbl; } private void button1_Click(object sender, EventArgs e) { OpenFileDialog opf = new OpenFileDialog(); opf.Filter = "Archivos CSV|*.csv"; if (opf.ShowDialog() == DialogResult.OK) { dataGrid1.DataSource = ConvertToDataTable(opf.FileName, 3); } opf.Dispose(); } private void dataGrid1_CurrentCellChanged(object sender, EventArgs e) { } private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { HitTestInfo hitInfo = dataGrid1.HitTest(e.X, e.Y); if (hitInfo.Type == HitTestType.Cell) { string selCell = dataGrid1[hitInfo.Row, hitInfo.Column]; if (cellData != null) this.label1.Text = selCell.ToString(); } } private void dataGrid1_CellDoubleClick(object sender, EventArgs e) //DataGridCellEventArgs { int c = dataGrid1.CurrentCell.ColumnNumber; int r = dataGrid1.CurrentCell.RowNumber; mstr = dataGrid1[c, r].ToString(); Form2 f2 = new Form2(); f2.ShowDialog(); } private void textBox1_TextChanged(object sender, EventArgs e) { BindingSource bs = new BindingSource(); bs.DataSource = dataGrid1.DataSource; bs.Filter = "NOMBRE like '%" + textBox1.Text + "%'"; dataGrid1.DataSource = bs; } }
}
en form2 private void Form2_Load(object sender, EventArgs e) { //Form1 f1 = new Form1(); //textBox1.Text = f1.mstr; textBox1.Text = Form1.mstr; }