0
votes

I want datagridview value to appear on 'new entry forms' (textboxes but also combobox) when I click on it.

This is my code which works but only for textboxes because i don't know how to get GENDER value 'out from' datagridview back to combobox.

My combobox values are "M" and "F" and i know how to enter them into datagridview but the other way round is problem.

private void dataGridViewKontakti_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        lblID.Visible = true;
        lblIDPromjena.Visible = true;

        int i;
        i = dataGridViewKontakti.SelectedCells[0].RowIndex;

        lblIDPromjena.Text = dataGridViewKontakti.Rows[i].Cells[0].Value.ToString();
        textBoxIme.Text = dataGridViewKontakti.Rows[i].Cells[1].Value.ToString();
        textBoxPrezime.Text = dataGridViewKontakti.Rows[i].Cells[2].Value.ToString();
        textBoxZvanje.Text = dataGridViewKontakti.Rows[i].Cells[3].Value.ToString();
        textBoxZanimanje.Text = dataGridViewKontakti.Rows[i].Cells[4].Value.ToString();

        textBoxTelefon.Text = dataGridViewKontakti.Rows[i].Cells[6].Value.ToString();
        textBoxAdresa.Text = dataGridViewKontakti.Rows[i].Cells[7].Value.ToString();
        textBoxPost.Text = dataGridViewKontakti.Rows[i].Cells[8].Value.ToString();
        textBoxGrad.Text = dataGridViewKontakti.Rows[i].Cells[9].Value.ToString();
        textBoxEmail.Text = dataGridViewKontakti.Rows[i].Cells[10].Value.ToString();

You'l see the empty space between cells 4 and 6. Cell 5 is my GENDER cell.

1
I'm not exactly sure what you are doing/asking here. If you are pulling a value from a DataGridView, please show us what value you are expecting, what value you are receiving, and if you get an exception, where that is happeningMatthew Frontino
I am registering new user with his Name, Surname, Title, Job, GENDER (with combobox values M or F), telephone, adress, post office, city and e-mail. The thing I need to do is get values from datagridview appear back in my textboxes (but also in combobox) when I click on one row in it. The code i wrote gets values "back" into textboxes, but I also need my GENDER value (M or F) to appear on my combobox when I click on one row (and I don't know how to do that)oljko
In order for values to show in a combobox, you need to have a list of items in that combobox. Where is your code where you create the combobox? Did you set the value/display member? In your code you never access value 5? Did you even try it to see what happens? Can you show us what happens?? I cannot help you unless you show us more code for your applicationMatthew Frontino
//added 2 items in my combobox cbSpol.Items.Add("M"); cbSpol.Items.Add("F"); //Conversion into string private void cbSpol_SelectedIndexChanged(object sender, EventArgs e) { Convert.ToString(cbSpol.SelectedItem); } //INSERT string with kSpol for entering GENDER from combobox. String query = "INSERT INTO dbo.kontakti (kIme, kPrezime, kSpol, kTelefon, kAdresa, kPost, kGrad, kEmail) VALUES (kIme, kPrezime, kZvanje, kZanimanje, kSpol, ...."); ... command.Parameters.AddWithValue("kSpol", cbSpol.SelectedItem); ... (with @ beside parameters)oljko

1 Answers

0
votes

Based on your comments, I think you would need to set the SelectedItem on the ComboBox assuming your data has a string "M" and "F" in it:

    public Form1()
    {
        InitializeComponent();
        this.comboBox1.Items.Add("M");
        this.comboBox1.Items.Add("F");

        comboBox1.SelectedItem = "M";

    }