Configuring DataGridView Column

自作多情 提交于 2021-02-08 08:09:21

问题


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

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.


回答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:

  • Basic Column, Row, and Cell Features in the Windows Forms DataGridView Control
  • Basic Formatting and Styling in the Windows Forms DataGridView Control
  • Column Types in the Windows Forms DataGridView Control



回答2:


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



来源:https://stackoverflow.com/questions/40296855/configuring-datagridview-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!