配置DataGridView列 [英] Configuring DataGridView Column

查看:62
本文介绍了配置DataGridView列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个winforms项目,并且DataGridView遇到了问题,因为我以前从未使用过它.

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

问题1:我希望程序自动生成"MemberID",并且用户不应对其进行编辑.

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

问题2:我正在使用数据库,但我不希望显示"FamilyDetailFK",而是它应该自动是GUI另一部分中TextBox显示的值.

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.

问题3:用户是否可以从下拉菜单中选择关系",所以这意味着每个新条目都应具有关系"的下拉选项,并且用户只能从以下选项中进行选择它,而不是直接输入它.这将使信息的分类更加容易,并使数据库保持简单.

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.

问题4:如果"DOB"具有日历图标,则可以选择而不输入数据,这将很有帮助,这是为了避免用户出错并保持数据库均匀格式化.

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.

这是我必须显示DataGridView的工作代码:

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();
                }
            }
        }
    }

我已经做过一些研究,并且我知道DataGridView存在很多问题.如果有人给我发送一个可以解决任何问题的链接或提供有关如何解决这些问题的模板代码,我将不胜感激.

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.

推荐答案

要配置 DataGridView ,请考虑:

  1. 要将列设为只读,可以设置

  1. 要使列不可见,可以设置

  1. 如果添加自定义列类型,则可以设置其

    您还可以通过设置 DataSource DisplayMember ,将 DataGridViewComboBoxColumn 配置为支持像 ComboBox 这样的键/值.和 ValueMember .

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

    创建一个 CalendarColumn (基于msdn示例).然后像我们对组合框列所做的那样,将 DOB 字段的日历列添加到 dataGridView1 .

    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.

    要了解有关 DataGridView 的更多信息,请查看

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

    也来看看:

    这篇关于配置DataGridView列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆