|
To add to what Dave said, you should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.
We all make mistakes.
And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!
So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
It's like a cursor only larger and more complex.
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?
That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
By creating an object based on a "Control"; and by varying it's .Left and .Top properties at run time over a given interval.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Make a system that will allow the user to add, modify, display, search and delete records in a derived classes.
Base class is 'Person'
Subclasses of Person = Employee and Student
Under Employee we also have subclasses namely= teacher and staff member.
Please help me.
|
|
|
|
|
This is the C# forum, not C++. That's here[^].
Again, you'll have to describe the problem you're having. Just saying "I need help" will not do.
|
|
|
|
|
Again, two things:
1) This is a C# forum, not C++. The two languages look similar, but are very, very different.
2) While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
Just posting your assignment isn't going to get you anywhere.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Posting the same homework multiple times isn't going to get you more, help: it probably will get you less.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Make a system that will allow the user to add, modify, display, search and delete record in a derived class 'Student'
Base class is 'Person'
|
|
|
|
|
Did you have a question or a problem with your code?
We're not here to do your homework for you.
|
|
|
|
|
Two things:
1) This is a C# forum, not C++. The two languages look similar, but are very, very different.
2) While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
Just posting your assignment isn't going to get you anywhere.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I am trying to run a process which is invoked from either a C# or C++ program. The process has its standard output and standard input redirected to the program which invoked it. In this way, the calling program can control the child process programmatically. (The child process happens to be the MAME arcade emulator so I can run commands through the Lua console.)
Things look about how I would expect them from the C++ program. I can read the output from the child process with the PeekNamedPipe() function. The characters are identical to what I see when I just run the process from the command line and observe the console.
But when I run a similar C# program, the console output is not quite right. There are at least two issues. Using the Streamread read() method (the child process's stdout is directed to a stream), there are always about 30 "junk" characters that are read back before I see the characters I expect. Additionally, it appears that the characters at 16-bits (not 8-bit chars). (I can live with this, and I think I can convert them into 8-bit chars if I need to.)
I have a strong suspicion that these characters are actually some low-level "under the hood" info about the string itself, like some kind of reflection data - maybe details about how it's encoded, length, etc.
I just can't seems to figure out how to cleanly extract the "real" data out of the stream using C#.
Can anyone make sense of this and give a hint of how I can get ASCII character data from the child process in C#?
Thanks!
|
|
|
|
|
C# uses Unicode - and the console it writes to is also Unicode by default.
You may get better results using the Console.OutputEncoding Property[^] set to Encoding.ASCII , but I've never needed to try myself.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have tried the suggestions found here and elsewhere but still no joy!
DataTable dataTable = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dataTable.Columns.Add(col.Name);
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataRow drow = dataTable.NewRow();
foreach (DataGridViewCell cell in row.Cells)
{
drow[cell.OwningColumn.Name] = cell.Value;
}
dataTable.ImportRow(drow);
dataTable.AcceptChanges();
}
modified 23-Oct-22 6:13am.
|
|
|
|
|
If I use your code, but replace the ImportRow with your commented out Rows.Add code, it works fine for me:
private void MyOtherButton_Click(object sender, EventArgs e)
{
DataTable dataTable = new DataTable();
foreach (DataGridViewColumn col in myDataGridView.Columns)
{
dataTable.Columns.Add(col.Name);
}
foreach (DataGridViewRow row in myDataGridView.Rows)
{
DataRow drow = dataTable.NewRow();
foreach (DataGridViewCell cell in row.Cells)
{
drow[cell.OwningColumn.Name] = cell.Value;
}
dataTable.Rows.Add(drow);
dataTable.AcceptChanges();
}
MyOtherDataGridView.DataSource = dataTable;
}
I get identical DGV's side by side ...
So what am I doing that you aren't?
And why are you doing this ass backwards? Normally you use the DataGridView.DataSource to populate data, not the actual rows and columns ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Please forgive me for being a bit slow. I am 70 years old and have just started getting back to programming because I can't do much else these days.
My aim is to be able to save the DataGridview data to a spreadsheet using EPPlus.
This is the code to get the data into the spreadsheet from a data table.
using (ExcelPackage pck = new ExcelPackage(newFile))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Accounts");
ws.Cells["A1"].LoadFromDataTable(dataTable, true);
pck.Save();
}
I do not have any code to populate the spreadsheet directly from the DataGridView.
If you could show me how to hook up a DataTable to get the data from a DataGridView using a Datasource or another method I would be very thankful.
|
|
|
|
|
Start by looking at where you get the data into the DGV: you read it from somewhere into something, and add that to the DGV somehow - there are a number of ways so I can't tell you "do this and it'll work".
When you have that, let us know and we'll see where to go from that.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I am getting the data from SQL server express and then saving it as an xlsx file.
using the following code that works well.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
DataTable dataTable = new DataTable();
BindingSource SBind = new BindingSource();
if (openFileDialog1.FileName != null)
{
ExcelEPP workbook = new ExcelEPP();
dataTable = workbook.ReadFromExcel(openFileDialog1.FileName);
}
dataGridView1.Columns.Clear();
dataGridView1.Rows.Clear();
SBind.DataSource = dataTable;
dataGridView1.DataSource = SBind;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
}
|
|
|
|
|
So you already have your data in a DataTable - which you read from Excel, not Sql Express - you then use that to source data to a BindingSource, which you then use as the DataGridView data source.
Why not just follow the links back to the original DataTable?
if (myDGV.DataSource is BindingSource bs)
{
if (bs.DataSource is DataTable dt)
{
... use the Datatable in dt here ...
}
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
modified 23-Oct-22 12:00pm.
|
|
|
|
|
Thank you for that bit of code as it does allow the saving of the original data table that was loaded from the file.
It, unfortunately, does not include any edits made to the DataGriview after the load.
What am I missing to update the DataTable so that it includes the changes made to the DataGridView?
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.Filter = "Excel Worksheets (*.xlsx)|*.xlsx|xls file (*.xls)|*.xls|All files (*.*)|*.*";
saveFileDialog1.FileName = "";
if (dataGridView1.DataSource is BindingSource SBind)
{
if (SBind.DataSource is DataTable dataTable)
{
using (ExcelPackage package = new ExcelPackage())
{
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
{
try
{
ExcelWorksheet ws = package.Workbook.Worksheets.Add("Countries");
ws.Cells["A1"].LoadFromDataTable(dataTable, true);
package.SaveAs(new FileInfo(saveFileDialog1.FileName));
}
catch (Exception)
{
DialogResult reminder = MessageBox.Show("Cannot save file, file opened in another program.\nClose it first! ", "Save Failed", MessageBoxButtons.OK);
}
}
}
}
}
}
|
|
|
|
|
I just Needed to Refresh the DataGridview.
Thanks for your help, I coded for forty years and have forgotten most of it but if I keep at it I hope it will come back.
|
|
|
|
|
You're welcome!
Can I suggest that a book might help? There is a lot of stuff in C# that can be really helpful - but if you don't even know it exists, you can't use it! A book will cover the language and most of the framework so you get a good idea what is available even if you can't remember how to use it right at this moment?
For example little bits like declaring a local variable tb in a test:
if (sender is TextBox tb)
{
... Instead of the older style
TextBox tb = null;
if (sender is TextBox)
{
tb = (TextBox) sender;
... Or
TextBox tb = sender as TextBox;
if (tb != null)
{
...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Having a problem with one line of code to save only a DataGrid's Rows that have been Multi-selected.
The code works correctly to save all the rows to file, however code line 04 is incorrectly formatted
to save only those rows which have been multi-selected. Appreciate any help. With thanks.
// Export DataGridView Selected Rows to Text File
TextWriter writer = new StreamWriter(FilePathName);
if (dataGridView1.SelectedRows.Count != 0)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
for (int i = dataGridView1.Rows(row.Index); i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (j == dataGridView1.Columns.Count - 1)
{
writer.Write("\t" + dataGridView1.Rows[i].Cells[j].Value.ToString());
}
else
writer.Write("\t" + dataGridView1.Rows[i].Cells[j].Value.ToString() + "\t" + "|");
}
writer.WriteLine("");
}
}
writer.Close();
modified 23-Oct-22 0:09am.
|
|
|
|
|
Why do you even have line 4?
Take a look at your code again. Line 2 iterates over the SelectedRows collection, using the variable "row". now, where do you use that variable in the rest of your code? Hint: You don't. You're completely ignoring each of the SelectedRows. You go right back to looking at every row and column in the DGV.
Get rid of line 4, and rewrite the rest of the code to use the "row" variable instead. You don't need to start dereferencing with "dataGridView1.Rows[]". You can just use "row.Cells[j]..."
|
|
|
|
|
Thanks Dave. Appreciate your insight. My copy paste was incorrect, as this is the first time on the forum. Have updated what I trying to achieve.
All good and with thanks Dave.
modified 23-Oct-22 0:21am.
|
|
|
|
|
OK, the new code you posted is no different than the first version. You're doing the exact same thing and need to fix it the exact same way I already mentioned.
|
|
|
|