问题
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:
- To make a column read-only, you can set ReadOnly property of the column to
true
.
For example, here setdataGridView1.Columns["MemberId"].ReadOnly = true
- To make a column invisible, you can set Visible property of the column to
false
.
For example, here setdataGridView1.Columns["FamilyDetailFK"].Visible = false
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, addDataGridViewComboBoxColumn
: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 aComboBox
by setting itsDataSource
,DisplayMember
andValueMember
.Crate a CalendarColumn based on the msdn example. Then add a calendar column for
DOB
field todataGridView1
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