Data labels ?
-
sorry, my other post wasn't clear ... hello, i want to create a more complex highscore board that displays the playname and score in labels! currently i have a datagrid which is displaying my data as a test, here is my code for that:
OLEDataConnector Dat = new OLEDataConnector("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\"Highscore.accdb\\";Persist Security Info=False;"); DataTable Dt = new DataTable(); Dat.dataSelect("select Playername, Score, LevelNumber from score order by score desc", ref Dt); dataGridView1.DataSource = Dt.DefaultView;
its a microsoft access database and i need to get around 10 rows if they are in the database to be put onto my highscore board ... So what i want is the playername of the record to be put into one label and the other as a score in another label, obviosuly they will move around using the query, :P Any help? as i can't find anything on this that is of any use.... Thanks
-
sorry, my other post wasn't clear ... hello, i want to create a more complex highscore board that displays the playname and score in labels! currently i have a datagrid which is displaying my data as a test, here is my code for that:
OLEDataConnector Dat = new OLEDataConnector("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\"Highscore.accdb\\";Persist Security Info=False;"); DataTable Dt = new DataTable(); Dat.dataSelect("select Playername, Score, LevelNumber from score order by score desc", ref Dt); dataGridView1.DataSource = Dt.DefaultView;
its a microsoft access database and i need to get around 10 rows if they are in the database to be put onto my highscore board ... So what i want is the playername of the record to be put into one label and the other as a score in another label, obviosuly they will move around using the query, :P Any help? as i can't find anything on this that is of any use.... Thanks
Why use labels at all? Why not just get the data and draw the text on a panel yourself? You can't bind a list of data to automatically create an array of labels for you. You have to copy the text from each record from the database to each label where it belongs. It's easier to just draw it yourself.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
sorry, my other post wasn't clear ... hello, i want to create a more complex highscore board that displays the playname and score in labels! currently i have a datagrid which is displaying my data as a test, here is my code for that:
OLEDataConnector Dat = new OLEDataConnector("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\"Highscore.accdb\\";Persist Security Info=False;"); DataTable Dt = new DataTable(); Dat.dataSelect("select Playername, Score, LevelNumber from score order by score desc", ref Dt); dataGridView1.DataSource = Dt.DefaultView;
its a microsoft access database and i need to get around 10 rows if they are in the database to be put onto my highscore board ... So what i want is the playername of the record to be put into one label and the other as a score in another label, obviosuly they will move around using the query, :P Any help? as i can't find anything on this that is of any use.... Thanks
Well the
DataTable
that you have created in your code has a Rows[^] property. What you can do is iterate over this collection (there is an example in the link above) and for eachDataRow
, create your twoLabels
and add them to your form. You will have to keep track of the X and Y coordinates so that you can position them properly. I could write the code out for you, but that really wouldn't help you learn. Have a go and if you get stuck, or you don't understand, please come back to me. Good luck. :)Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Well the
DataTable
that you have created in your code has a Rows[^] property. What you can do is iterate over this collection (there is an example in the link above) and for eachDataRow
, create your twoLabels
and add them to your form. You will have to keep track of the X and Y coordinates so that you can position them properly. I could write the code out for you, but that really wouldn't help you learn. Have a go and if you get stuck, or you don't understand, please come back to me. Good luck. :)Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Well the
DataTable
that you have created in your code has a Rows[^] property. What you can do is iterate over this collection (there is an example in the link above) and for eachDataRow
, create your twoLabels
and add them to your form. You will have to keep track of the X and Y coordinates so that you can position them properly. I could write the code out for you, but that really wouldn't help you learn. Have a go and if you get stuck, or you don't understand, please come back to me. Good luck. :)Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
hello, i can't get it to work and i have spent another 3 hours trying differant ways lol, can you help me ?
Something like:
Label nameLabel = null;
Label scoreLabel = null;
int nameX = 10;
int nameY = 10;
readonly int nameWidth = 70; // Change this value to alter the spacing adjust scoreX accordingly, if you do
int scoreX = 85;
int scoreY = 10;
int listCount = 1;
foreach(DataRow row in dt.Rows)
{
nameLabel = new Label(); // Create the label
nameLabel.Width = nameWidth; // set its width (use a variable so that you can just alter that value to change the layout)
nameLabel.Text = row["PlayerName"].Value.ToString(); // you might need to check for null here, depends on your data.
nameLabel.Location = new Point(nameX, nameY);
this.Controls.Add(nameLabel)
nameY += nameLabel.Height + 5; // add 5 for spacing. You could use a variable as for the width. The X value stays the same so that the left edges line up// Do the same for scoreLabel ........................ ........................ ........................ ........................ listCount++; if (listCount > 10) // to ensure only the 10 as per your original post. { break; }
}
That is roughly right but I'm doing it off the top of my head and it is very late here, so forgive any syntax errors. Good luck. :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Something like:
Label nameLabel = null;
Label scoreLabel = null;
int nameX = 10;
int nameY = 10;
readonly int nameWidth = 70; // Change this value to alter the spacing adjust scoreX accordingly, if you do
int scoreX = 85;
int scoreY = 10;
int listCount = 1;
foreach(DataRow row in dt.Rows)
{
nameLabel = new Label(); // Create the label
nameLabel.Width = nameWidth; // set its width (use a variable so that you can just alter that value to change the layout)
nameLabel.Text = row["PlayerName"].Value.ToString(); // you might need to check for null here, depends on your data.
nameLabel.Location = new Point(nameX, nameY);
this.Controls.Add(nameLabel)
nameY += nameLabel.Height + 5; // add 5 for spacing. You could use a variable as for the width. The X value stays the same so that the left edges line up// Do the same for scoreLabel ........................ ........................ ........................ ........................ listCount++; if (listCount > 10) // to ensure only the 10 as per your original post. { break; }
}
That is roughly right but I'm doing it off the top of my head and it is very late here, so forgive any syntax errors. Good luck. :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”