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.