So.... I have created a new project, I have added a database and dataset, created a table, populated it, set it as a data source, and finally have pulled a 'details' and 'datagridview' onto my form.
I have tested these and all displays just fine.
Now the problem. I want to call a cell value and then carry out a task based on the result, but everytime I try, I get an out of range exception.
I've looked around at other topics and tutorials and similar questions and it suggests that I'm trying to call a cell that doesnt exist. This doesnt make sense, as I've tried various low-number index combinations, and my table consists of 4 columns and 16 rows, all of which contain data.
All other indicators point to the program not understanding that the datagridview actually contains data - as if it wants to me to define it before it can call the data - but this makes no sense to me because I've pulled it from a database and I can view the data with no problems when I run the program.
I've tried various different codes to call on a cell, but I get the same exception every time.
int testValue1 = (int)islandMapDataGridView[1, 1].Value;
Console.WriteLine(testValue1);
I should point out that I'm reasonably new to this. What am I doing wrong?
EDIT - Thank you for pointing me to a similar question with a lengthy answer - unfortunately I've looked at many just like this already. They are suggesting that I'm picking a cell that does not exist.
I am aware that these indexes start at 0 and I should compensate for this. Again, my datagridview has 4 columns and 16 rows. I am (for example) attempting to get the data at cell index 1,1 - which is well within the range of the datagridview.
The problem does not seem to be that I am calling a cell that falls outside the range of cells and rows in the datagridview but rather that it seems to think that nothing is there at all. But if I disable the line of code that is supposed to return the value - when I run the program I can see the table just fine and the data is there.
If I have to take the time to define the datagridview just so I can call a value from a cell, then what was the point in setting up databases, datasets, datasources and tables in the first place?
2nd EDIT - What do I have to do to get this looked at again????? Somebody has stated this is an exact duplicate of another question, and linked me to it. It is NOT an exact duplicate. I am NOT trying to reference a cell that falls outside of the datagridview. The problem seems to be that the datagridview is not properly reporting back the data contained within in. Presumably I have missed some sort of initialisation, or there is something wrong with the binding.... I dont know... If I knew, then it wouldnt be a problem. The linked topic/question does nothing to explain what I am missing. Please can I get some help with this? Please?
3rd Edit - I'd like to add that since my opening question, I've been looking around and trying to find a solution - I stumbled across a tutorial carrying out a similar exericse, and they included the following line of code:
islandMapDataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
I hoped this might resolve my problem, but sadly it did not.
I am guessing that something fundamental is missing that allows me to report values from the datagrid, or it is somehow not properly loaded or configured.
I would appreciate any help - even if just to point me a tutorial or similar which expains how to set this sort of thing up.
Do I need to set up a class in order to fetch a value from a cell? Do I need to set up a database context? Please, what am I missing here?
4th Edit! - Ok so here is the code that was automatically generated by the program when i dragged in the 'datagridview' and 'details' from the table in datasources.
private void islandMapBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.islandMapBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.starterIslandDataSet);
}
private void Form4_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'starterIslandDataSet.IslandMap' table. You can move, or remove it, as needed.
this.islandMapTableAdapter.Fill(this.starterIslandDataSet.IslandMap);
}
There doesnt appear to be anything specifically here for the datagridview - but again, when i run the program, it displays it just fine, including all the information that i entered into the table.
Here is the code I am trying to use to pull info from the database -
private void MapTilesTest()
{
islandMapDataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
foreach (Control control in tableLayoutPanel2.Controls)
{
PictureBox maptile = control as PictureBox;
if (maptile != null)
{
int testValue1 = (int)islandMapDataGridView[1, 1].Value;
Console.WriteLine(testValue1);
}
}
I am using console.writeline just to confirm that i can indeed pull a value from the table.
I have looked at tutorials for advice, and i see lots where they are manually coding the datagridview and manually inputting the data into the datagridview in the code - i assume this is one way of doing it - but i cant find a good example where someone is pulling information from a database in the same way.