I have a DataGridView in my applicaiton, but I want to limit it to 20 rows only, however because it is data-bound, I get the InvalidOperationException
. Through research I have come across virtual mode, but this looks incredibly confusing for a starter like me. Furthermore, I have seen people say setting the DGV.DataSource = null;
to solve the problem, but this doesn't work for me.
I basically, click a button, and then the DataGridView populates with information. The only work around I have in place, is limiting the user to enter only 20 instances of my object. Then they have to delete one in order to enter another.
Is there any way I can set a maximum row limit with a data-bound DataGridView?
EDIT
In my separate class file (objectStorage.cs) I have this
public class objectStorage: IComparable<objectStorage>
{
public string objID { get; set; }
public string objName { get; set; }
public string objDescription { get; set; }
public static List<objectStorage> objArray = new List<objectStorage>();
}
Then in my form where I add a new object (addObj.cs) I have this:
private void objCreationBtn_Click(object sender, EventArgs e)
{
objectStorage.objArray.Add(new objectStorage
{
objID = objIDValueTxt.Text,
objName = objNameRTB.Text,
objDescription = objDescRTB.Text,
});
}
Then In my main form I have the DGV (Where I assigned the data source to objectStorage in the GUI, and thus created a binding source) which I have created a method to show the objects, like this in a button press:
private void displayObjBtn_Click(object sender, EventArgs e)
{
showObjects();
}
private void showObjects()
{
objectStorageBindingSource.DataSource = null;
objectStorageBindingSource.DataSource = objectStorage.objArray;
}
This works completely okay, but obviously includes no limit to the row count, and I cannot do this because of the data-bound problem.