Note that there are some explanatory texts on larger screens.

plurals
  1. POC# and entity framework objects
    text
    copied!<p>try to made my first c# programme, with vs2010 and entity framework. This is the code:</p> <pre><code>public partial class Form1 : Form { ValinorEntities ve = new ValinorEntities(); public Form1() { InitializeComponent(); comboBox1.DataSource = ve.item_groups; comboBox1.DisplayMember = "item_group_name"; comboBox1.ValueMember = "item_group_id"; var departmentQuery = from d in ve.items from g in ve.item_groups where d.item_group_id_fk == g.item_group_id select new { d.item_name, g.item_group_name, d.item_id, d.item_number }; dataGridView1.DataSource = departmentQuery; dataGridView1.Columns[2].Visible = false; dataGridView1.Columns[3].Visible = false; } private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString(); textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(); comboBox1.DataSource = ve.item_groups; comboBox1.DisplayMember = "item_group_name"; comboBox1.ValueMember = "item_group_id"; } private void btn_New_Click(object sender, EventArgs e) { try { item item = new Valinor.item(); item.item_name = textBox1.Text; item.item_number = textBox2.Text; item.item_group_id_fk = (int)comboBox1.SelectedValue; ve.AddToitems(item); ve.SaveChanges(); MessageBox.Show("Insert successful"); } catch (Exception ex) { MessageBox.Show("Insert failed" + ex); } } private void btn_Save_Click(object sender, EventArgs e) { try { var id = Convert.ToInt32(textBox3.Text); item c = ve.items.FirstOrDefault(i =&gt; i.item_id == id); c.item_name = textBox1.Text; c.item_number = textBox2.Text; c.item_group_id_fk = (int)comboBox1.SelectedValue; ve.SaveChanges(); MessageBox.Show("Save successful"); } catch (Exception ex) { MessageBox.Show("Save failed" + ex); } } } </code></pre> <p>It's working but i have a few problem.</p> <ul> <li>What should I write a <code>comboBox1.SelectedValue</code> to the correct item is selected after clicking on a cell?</li> <li><p>In the <code>btnSave_Click</code> function the value of<code>textbox3</code> refer to ID, which is not very elegant solution. How can I object to replace it?</p></li> <li><p>If I know of such references, such as these:</p> <p><code>textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();</code> <code>textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();</code></p></li> </ul> <p>could be avoided somehow. To do this, however zakell that <code>dataGridView1_CellMouseClick</code> fügvénynél not refer the value of the cell but also the object. This can be to solve it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload