1

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;
                try
                {
                    connection.Open();
                    command.Parameters.Add("@Id", SqlDbType.Int).Value = txtfamilyNumber.Text;
                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    adapter.SelectCommand = command;

                    DataTable Dt = new DataTable();
                    adapter.Fill(Dt);
                    primaryGridView.DataSource = Dt;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK);
                }
                finally
                {
                    connection.Close();
                }
            }
        }
    }

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Albin Vinoy
  • 381
  • 1
  • 5
  • 14
  • You should ask a specific single question in a single post but currently you have asked 4 questions. For more information see [One post with multiple questions or multiple posts?](http://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts) I considered this question a general question about how to perform some configurations on DataGridView columns and answered it to share some useful tips and links. – Reza Aghaei Oct 28 '16 at 06:58

2 Answers2

1

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"});
    dataGridView1.Columns.Add(C1);
    

    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:

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

It is possible to subclass both the DataGridViewColumn and DataGridViewCell classes to host any control of your choice and To make the text portion of a ComboBox non-editable, set the DropDownStyle property to "DropDownList". and to make a particular column non-editable you can this.ColumnName.ReadOnly = True

Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69