Click here to Skip to main content
15,917,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
With the code I load records in a data grid view. Now I want to add the IDMaterieel (ID field) to the row in order to store the selectedID (row) in a label field for other purposes.

like >>> listBox.ValueMember / listBox.SelectedValue option in using a listbox.

what is the easiest way to do this?


C#
private void Filteren()
{
    string queryMaterieel = "SELECT [IDMaterieel],IPNummer, Materieelstuk From Materieel WHERE Materieelstuk like @ZoekMaterieel AND IPNummer like @ZoekMaterieelnr ORDER BY IPNummer";

    using (connection = new SqlConnection(connectionstring))
    using (SqlCommand command = new SqlCommand(queryMaterieel, connection))
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        try
        {
            //TX_MaterieelZoeken.Text = TX_MaterieelZoeken.Text.Replace(" ", "%");
            command.Parameters.AddWithValue("@ZoekMaterieel", "%" + TX_MaterieelZoeken.Text + "%");
            command.Parameters.AddWithValue("@ZoekMaterieelnr", "%" + TX_MaterieelNRZoeken.Text + "%");

            DataTable Materieellijst = new DataTable();
            adapter.Fill(Materieellijst);

            DGV_Materieel.ColumnHeadersVisible = false;
            DGV_Materieel.DataSource = Materieellijst;
            MaterieelstukbewerkenToolStripMenuItem.Enabled = true;

            if (DGV_Materieel.Rows.Count == 0)
            {
                MaterieelstukbewerkenToolStripMenuItem.Enabled = false;
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Er is een onbekende fout opgetreden. De door u ingevulde gegevens zijn niet verwerkt.", "** | Error - E111", MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
}


What I have tried:

I only know how to solve this with a listbox.

I already oriented on msdn.microsoft.com but without result.
Posted
Comments
Sinisa Hajnal 10-Feb-16 6:15am    
Just add the field to your datagridview. Not all bound fields have to be visible. You can access it just as any other field, except it will not be visible to the user.

1 solution

Here is the link as how to hide the column -
http://stackoverflow.com/questions/3819247/gridview-hide-column-by-code

Using the above trick , I have modified your code -

C#
private void Filteren()
{
    string queryMaterieel = "SELECT [IDMaterieel],IPNummer, Materieelstuk From Materieel WHERE Materieelstuk like @ZoekMaterieel AND IPNummer like @ZoekMaterieelnr ORDER BY IPNummer";
 
    using (connection = new SqlConnection(connectionstring))
    using (SqlCommand command = new SqlCommand(queryMaterieel, connection))
    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
        try
        {
            //TX_MaterieelZoeken.Text = TX_MaterieelZoeken.Text.Replace(" ", "%");
            command.Parameters.AddWithValue("@ZoekMaterieel", "%" + TX_MaterieelZoeken.Text + "%");
            command.Parameters.AddWithValue("@ZoekMaterieelnr", "%" + TX_MaterieelNRZoeken.Text + "%");
 
            DataTable Materieellijst = new DataTable();
            adapter.Fill(Materieellijst);
 
            DGV_Materieel.ColumnHeadersVisible = false;
            DGV_Materieel.DataSource = Materieellijst;
            DGV_Materieel.Row.Cells[0].Visible = false;
            MaterieelstukbewerkenToolStripMenuItem.Enabled = true;
 
            if (DGV_Materieel.Rows.Count == 0)
            {
                MaterieelstukbewerkenToolStripMenuItem.Enabled = false;
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Er is een onbekende fout opgetreden. De door u ingevulde gegevens zijn niet verwerkt.", "** | Error - E111", MessageBoxButtons.OK, MessageBoxIcon.Hand);
        }
}</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900