What I want to do is add videos to a list available for rent.

In the “main” form when I click “Add” then the frmAddVideo appears where I need to insert all the video’s information. (This works 100% but…) In the frmAddVideo form there is a comboBox with values “Yes” and “No” for the availability of the video. 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.

Also when I click "View" on the main form I can view all the video's information. In this form it gives all the video's info and the availability must say "Yes" in the ComboBox if the video is checked in the dataGridView.

How can I do all this with a bool statement for availability and how will it look in my class? I provided code but I don’t think it is correct seeing that it does not work. I am not providing the main form because it is not necessary.

Video class

namespace A6
{
    [Serializable]

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

        //bool mAvailability;


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

        //DO AVAILABILITY


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

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

                //STILL HAVE TO IMPLEMENT THE AVAILABILITY???

                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.BOOL METHOD??);
        }
    }
}

The rest of the application works perfectly. Just can't figure out what to do with the bool statement for availability.

Thanks a lot guys!

J

Dani AI

Generated

's point is correct and 's quick fix works. A clearer, more robust pattern is to store availability as a true boolean on the Video class and bind UI controls to that property instead of comparing strings.

Add a boolean property (example name differs from the thread's earlier snippets) and set a sensible default in the constructor:

public bool IsAvailable { get; set; } = true;

For the grid: let the DataGridView show the bool as a checkbox (AutoGenerateColumns will do this automatically), or create a checkbox column and bind it explicitly:

dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = videoList; // e.g. BindingList<Video> or a BindingSource
var chk = new DataGridViewCheckBoxColumn {
    DataPropertyName = "IsAvailable",
    HeaderText = "Available"
};
dataGridView1.Columns.Add(chk);

For the Add/View forms prefer binding the ComboBox to true/false values instead of relying on the literal strings "Yes"/"No":

comboBoxAvailability.DisplayMember = "Text";
comboBoxAvailability.ValueMember = "Value";
comboBoxAvailability.DataSource = new[] {
    new { Text = "Yes", Value = true },
    new { Text = "No",  Value = false }
};
// To show a video's state:
comboBoxAvailability.SelectedValue = video.IsAvailable;
// To save:
video.IsAvailable = (bool)comboBoxAvailability.SelectedValue;

Troubleshooting tips: ensure DataPropertyName matches the property name exactly; use a BindingList<Video> or BindingSource for editable lists; check for null SelectedValue before casting; and prefer if (video.IsAvailable) rather than comparing to true. This keeps the logic simple and avoids fragile string comparisons.

Recommended Answers

All 3 Replies

I'm not entirely sure I understand the problem. A bool property will display appropriately as a checkbox by default in a DataGridView. So you'd add such a property:

bool Availability { get; set; }

Then convert the result of the combobox as necessary (varies depending on how you've got the combobox set up):

// If the items are the strings "Yes" and "No"
Newvideo.Availability = comboBox2.SelectedItem.ToString() == "Yes";

Haha, that pretty much did the trick...

But now the problems lies with when I view the video information in the other form (not on the dataGrid) I can't seem to get to display when the checkbox is checked it must display yes. And when it is unchecked it must say no.

Something like this:

            if (NewVideo.Availability.ToString() == "Yes")
            {
                comboBox2.SelectedItem = "Yes";
            }
            else
            {
                comboBox2.SelectedItem = "No";
            }

but its not working?

No matter. I figured it out by doing this.

if (NewVideo.Availability == true)
{
    comboBox2.SelectedItem = "Yes";
}
else
{
    comboBox2.SelectedItem = "No";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.