Albin Vinoy Albin Vinoy - 1 month ago 12
C# Question

Configuring DataGridView Column

I am working on a winforms project and I am having issues with DataGridView as I have never used it before.

This is the image of the DataGridView

Issue 1: I have want the program to auto generate "MemberID" and it should not be editable by the user.

Issue 2: I am working with database and I do not want "FamilyDetailFK" to be displayed, instead it should automatically be the value displayed by a TextBox in another part of the GUI.

Issue 3: Is there any way the user can select "Relationship" from a drop down menu, so that means each of the new entry should have a drop down option for "Relationship" and users should only be able to select from it and not directly input into it. This would allow sorting of information much easier and will keep the database simple.

Issue 4: It would be helpful if the "DOB" has a calender icon so the data can be chosen and not entered, this is to avoid errors by the user and to keep the database formatted in evenly.

This is the working code that I have to display the DataGridView:

private void loadgridview()
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString))
using (SqlCommand command = new SqlCommand())
command.CommandText = @"
SELECT MemberID, [Member Name], Relationship, DOB, [Place of Birth], FamilyDetailFK
FROM dbo.PrimaryDetail
WHERE dbo.PrimaryDetail.FamilyDetailFK = @id;
command.Connection = connection;
command.Parameters.Add("@Id", SqlDbType.Int).Value = txtfamilyNumber.Text;
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.SelectCommand = command;

DataTable Dt = new DataTable();
primaryGridView.DataSource = Dt;
catch (Exception e)
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK);

I have done some research and I know that there are lots of issues that I have with this DataGridView. I would really appreciate if someone would send me a link that addresses any of the issue or provide a template code on how to get solve them.


To configure the DataGridView, Consider:

  1. To make a column read-only, you can set ReadOnly property of the column to true.
    For example, here set dataGridView1.Columns["MemberId"].ReadOnly = true
  1. To make a column invisible, you can set Visible property of the column to false.
    For example, here set dataGridView1.Columns["FamilyDetailFK"].Visible = false
  1. If you add a custom column type which you set its DataPropertyName to a specific field name, the control will use that column type fod showing that field.
    For example, here before setting data source of the control, add DataGridViewComboBoxColumn:

    var relationshipColumn = new DataGridViewComboBoxColumn()
        {DataPropertyName="Relationship", Name="relationshipColumn"};
    relationshipColumn.Items.AddRange(new string[]{"self", "other", "some other"});

    You can also configure DataGridViewComboBoxColumn to support key/value like a ComboBox by setting its DataSource, DisplayMember and ValueMember.

  2. Crate a CalendarColumn based on the msdn example. Then add a calendar column for DOB field to dataGridView1 like what we did for combo box column.

To learn more about DataGridView, take a look at DataGridView Control (Windows Forms). It contains links to some documentations and useful How To articles.

Also take a look at: