【问题标题】:CheckBox in dataGridViewdataGridView 中的复选框
【发布时间】:2014-09-05 13:41:10
【问题描述】:

添加文件后 - 我想要一个选中的复选框,而不是在 dataGridView 列中看到“是”。我该怎么做?

我在做什么:

在“主”表单中,当我单击“添加”时,frmAddVideo 会出现在我需要插入所有视频信息的位置。在 frmAddVideo 表单中,有一个组合框,其值为“是”和“否”,用于表示视频的可用性。 When “Yes” is selected then the video displayed in the dataGridVideo after it is added, there must be a “ticked” checkbox in the dataGridView and vice versa for when it’s not available.

我提供了代码,但看到它不起作用,我认为这是不正确的。我没有提供主要形式,因为它没有必要。

frmAddVideo

namespace A6
{
    public partial class FrmAddVideo : Form
    {
        internal Video NewVideo;
        public FrmAddVideo()
        {
            InitializeComponent();
        }

        //ADD VIDEO
        private void button1_Click_1(object sender, EventArgs e)
        {
                NewVideo = new Video();

                NewVideo.Title = textBox1.Text;
                NewVideo.Category = Convert.ToString(comboBox1.SelectedItem);
                NewVideo.YearReleased = Convert.ToInt32(numericUpDown1.Value);
                NewVideo.RunTime = Convert.ToDouble(textBox2.Text);

                //AVAILABILITY
                //How do I make this to apear as a checkbox in the dataGridView?
                NewVideo.Availability = Convert.ToString(comboBox2.SelectedItem);

                if (NewVideo.yesNo() == true)
                    MessageBox.Show("Yes");
                else
                    MessageBox.Show("No");


                MessageBox.Show("Video added");
                this.Close();
        }
    }
}

frmViewVideo

namespace A6
{
    public partial class frmViewVideo : Form
    {
        internal Video NewVideo;
        public frmViewVideo()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmViewVideo_Load(object sender, EventArgs e)
        {
            textBox1.Text = NewVideo.Title;
            textBox2.Text = Convert.ToString(NewVideo.RunTime);
            numericUpDown1.Value = NewVideo.YearReleased;
            comboBox1.SelectedItem = Convert.ToString(NewVideo.Category);
            comboBox2.SelectedItem = Convert.ToString(NewVideo.Availability);
        }
    }
}

namespace A6
{
    [Serializable]

    class Video
    {
        private string mTitle;
        private string mCategory;
        private int mYearReleased;
        private double mRunTime;
        private string mAvailability;

        //bool mshowing3D;

        public string Title
        {
            get { return mTitle; }
            set { mTitle = value; }
        }

        public string Category
        {
            get { return mCategory; }
            set { mCategory = value; }
        }

        public int YearReleased
        {
            get { return mYearReleased; }
            set { mYearReleased = value; }
        }

        public double RunTime
        {
            get { return mRunTime; }
            set { mRunTime = value; }
        }

        public string Availability
        {
            get { return mAvailability; }
            set { mAvailability = value; }
        }

        public bool yesNo()
        {
            if (mAvailability == "Yes")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //CONSTRUCTOR
        public Video()
        {
            mTitle = "No Name";
            mCategory = "No Category";
            mYearReleased = 0;
            mRunTime = 0;
        }
    }
}

应用程序的其余部分运行良好。只是无法弄清楚如何处理可用性的 bool 语句。

非常感谢大家!

J

【问题讨论】:

  • 您发布了很多代码,但我在其中的任何地方都找不到您的 DataGridView 控件。即使您的添加视频代码也没有显示它被添加到任何东西。让您的可用性属性是一个字符串可能是一个错误,因为您将其视为布尔值。

标签: c# winforms checkbox datagridview


【解决方案1】:

首先在您的表单中创建一个新复选框,如下所示:

<asp:CheckBox ID="SelectCheckBox" runat="server" />

然后将 MessageBox 控件更改为 CheckBox 并将值传递给其选中的属性:

//AVAILABILITY
                //How do I make this to apear as a checkbox in the dataGridView?
                NewVideo.Availability = Convert.ToString(comboBox2.SelectedItem);

                                SelectCheckBox.Checked = NewVideo.yesNo();

【讨论】:

  • 您也可以让用户无法更改复选框,在您的 CodeBehind 中将其 Enabled 属性设置为 false;
  • 还有另一种创建复选框的方法吗?我问是因为我目前没有使用服务器或任何数据库。
  • 问题被标记为Winforms
  • 您可以像这样在 codeBehind(c# 文件)中创建一个复选框:CheckBox chk = new CheckBox();,然后您可以遍历其属性。
猜你喜欢
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多