|
Hi Griff
The code worked a treat, thank you.
I just adjusted it to work with my text boxes instead of labels and hey presto.
Hopefully this info will help another poor soul in the future.
Many thanks
Ron
|
|
|
|
|
You're welcome!
"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!
|
|
|
|
|
And if you're really lucky there is an extremely simple approach that takes only a single line of code:
let's assume you have say 9 textboxes and 9 labels telling what the textboxes are for; let's hope they are all organized in two columns, so all neatly atop each other.
Now add a Panel to your Form, covering all the space where you want the labels to go, but not covering anything else (that is where you need to be lucky). Now put the labels on the Panel, not on the Form itself. You can now make all labels (in)visible by changing the visibility of the Panel! And that takes a single line of code...
Luc Pattyn [My Articles]
If you can't find it on YouTube try TikTok...
|
|
|
|
|
Hi Luc
Thanks for the tip, amazing how the simplest of idea's are staring you in the face, lol!
To be honest i need to make other changes too, like updating the text on each with a different value.
I'm working on a big project to display register values of a chip.
What i was hoping to do was use some kind of simple for loop to access the label name seeing that the names have a digit at the end, then update the properties when needed.
Cheers
Ron
|
|
|
|
|
If you subclass the label, or make the equivalent user control, you can add code to the constructor and / or loaded / initialized events to automatically "register" the label in a (static) collection that you can iterate at will at run time.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I have wrote some lines of code for exporting DataGridView contents to Excel file. The code works fine in the main Form. I want to make a class (.dll file) from my code so I could use it as reference in many projects. How is it possible?
My code is:
private void btnExport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "pmInfo";
for (int i = 1; i < dataGridView1.Columns.Count+1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
var saveFileDialoge = new SaveFileDialog();
saveFileDialoge.FileName = "Output";
saveFileDialoge.DefaultExt = ".xlsx";
if (saveFileDialoge.ShowDialog()==DialogResult.OK)
{
workbook.SaveAs(saveFileDialoge.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
}
|
|
|
|
|
|
I pasted this code in the Class, but "Columns" and "Rows" have red underline. I cannot compile the class.
|
|
|
|
|
There is quite a bit missing from that code to make it into a dll. I suggest you follow the link I gave you and read up on how to make a class library.
|
|
|
|
|
First, make it as generic as possible: instead of writing it as an Event handler which works with a specific DataGridView and Sheet make it a generic routine that takes several parameters: The data source, the Sheet name, and the file path to write to. You call that generic method from your handler.
Ideally, you shouldn't pass a control like a DGV, but the underlying datasource it uses instead. You can access the properties from that quite easily - this shows you how: Converting a List to a DataTable[^]
You should not be using a dialog in a dll, as there is no guarantee that the calling application will be running under windows - it potentially could be a web app or on a server, where the user can't see a SaveFileDialog, and shouldn't have access to the file system anyway.
Then create a new Project as a ClassLibrary and move your method into it.
Add a reference (and using statement if necessary) to the library to your original project, and you can call it from there.
"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 tried the following class code. Now, my problem is that "Rows" and "Columns" have red underline. They have not been defined in my class while I have used Microsoft Office Excel 16 object reference.
How can I sole those red underlines?
My updated code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DataGridView
{
class ExcelExport
{
public void Export(string fileName, string DataSource,string FileName)
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = fileName;
for (int i = 1; i < DataSource.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = DataSource.Columns[i - 1].HeaderText;
}
for (int i = 0; i < DataSource.Rows.Count; i++)
{
for (int j = 0; j < DataSource.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = DataSource.Rows[i].Cells[j].Value.ToString();
}
}
var saveFileDialoge = new SaveFileDialog();
saveFileDialoge.FileName = FileName;
saveFileDialoge.DefaultExt = ".xlsx";
if (saveFileDialoge.ShowDialog() == DialogResult.OK)
{
workbook.SaveAs(saveFileDialoge.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
}
}
}
|
|
|
|
|
Look at your code:
public void Export(string fileName, string DataSource,string FileName)
{
...
for (int i = 1; i < DataSource.Columns.Count + 1; i++) Why do you think that a string would have rows or 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!
|
|
|
|
|
Yes, I notice my fault later. But I tried to create an DGV object like this:
private DataGridView DataSource = new DataGridView();
But it shows red underline under DataGridView. It says, DataGridView is a namespace.
|
|
|
|
|
Why are you trying to create a DataGridView at all? Do you expect magic to happen and it to be filled with data that exists in a different DataGridView?
Why do I get the feeling that you have no idea what you are doing, and are so far out of your depth that you are just guessing what to try? You didn't write that Excel code at all, did you?
"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 was a tutorial. Please help me. What should I do?
|
|
|
|
|
Pass the actual DataGridView - or better the DataSource it was loaded from - and use 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!
|
|
|
|
|
Can you write it in a line of code? Please
|
|
|
|
|
You are kidding, right?
You don't know how to pass a parameter?
myMethod(datatGridView1);
As far as using the data source instead goes, I have no idea how you are filling your DGV - but I suspect you don't either ...
"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!
|
|
|
|
|
datatGridView1 has not defined in Class view. There is no link between my main project and the class for datatGridView1.
|
|
|
|
|
We are going to be here all day, I suspect ...
You have a DGV in one form.
It contains data which it is displaying.
That data is organised into rows and columns.
Somewhere in your code, you added data to that DGV, be it by creating individual columns and then adding rows, or by creating a DataTable / Collection which held the data and setting that as the DataSource for the DGV.
You want to use the data in the DGV in a separate method (in a separate Assembly which is a DLL) so that it can output data to an XLSX file.
That means you have to pass the DGV or it's source data to the method in some way via a parameter, or the method has no way to access it and no idea what to write to the file.
Are you with me so far?
"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!
|
|
|
|
|
Message Closed
modified 17-Nov-20 1:59am.
|
|
|
|
|
Excellent.
First off, change the way you fill your DGV. Instead of adding columns and rows directly, create a DataTable instance instead, populate that, adn set that as the DataSource property of the DGV.
The DGV will now display the data and update it as you add items to the underlying DataTable.
When you want to output the data to XLSX, pass the DataTable instance to your method:
private DataTable theSource = new DataTable();
private void FrmMain_Shown(object sender, EventArgs e)
{
theSource.Columns.Add("Column 1");
theSource.Columns.Add("Column 2");
theSource.Rows.Add("cccc1111", 666);
myDataGridView.DataSource = theSource;
}
private void AddButton_Click(object sender, EventArgs e)
{
theSource.Rows.Add(DateTime.Now, "hello");
}
private void OutputButton_Click(object sender, EventArgs e)
{
MyDLLAssemblyClass.OutputToXLSX(theSource, @"d:\Test Data\myFile.xslx");
}
private static void OutputToXLSX(DataTable dt, string outputPath)
{
...
}
You can then just add a using line for System.Data to your DLL assembly class and use the Row and Column from the DataTable.
Now your DLL file doesn't need to know what the heck the rest of your code is doing, it just wants a DataTable with the data to output.
"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!
|
|
|
|
|
Oh, and do yourself a favour: stop using /* ... */ to comment methods.
Use /// instead, and VS will fill in the blanks ready for you to add actual information:
public static string ReadInstanceData(string storageClass)
{ Fill it in:
public static string ReadInstanceData(string storageClass)
{ And now VS can use that for Intellisence if you let it ...
"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 been working on this for some time now. Google can only take me so far...
So I thought I'd ask you guys and girls for some help...
----
I have a simple C#, WPF application that I've been playing around with to learn C# and WPF.
The code below is working, but its not displaying my menu items correctly. Here is an example of what it looks like (example A) and what it should look like (example b)
---------------------------------------------------------------------------------------
Example A:
---------------------------------------------------------------------------------------
File | Users | Settings | Tools | Help | Login | Exit | Managers | Window1 | Window2
---------------------------------------------------------------------------------------
Example B:
---------------------------------------------------------------------------------------
File | Users | Settings | Tools | Help Login
Exit | | Managers
| Window1
| Window2
---------------------------------------------------------------------------------------
Exit is a ChildItems \ sub menu items of File
Where as Managers, Window1, Window2 are ChildItems of Tools
---------------------------------------------------------------------------------------
My code below is working to a point
1) For my "mainItem", top level MenuItems, it's pulling them from the database correctly and displaying them correctly
2) For my "childItem", the menu items are pulling correctly from the database, but they are not being assigned or linked to their parent menu item correctly.
This is where I think I need some help from you!
---------------------------------------------------------------------------------------
public void PopulateMenu()
{
// MAIN MENU
Grid.SetRow(mainMenu, 0);
mainMenu.IsMainMenu = true;
mainMenu.Padding = new Thickness(5);
mainMenu.FontSize = 14;
mainMenu.HorizontalAlignment = HorizontalAlignment.Stretch;
mainMenu.VerticalAlignment = VerticalAlignment.Top;
// MENU ITEMSPANEL
mainMenu.ItemsPanel = new ItemsPanelTemplate(
new FrameworkElementFactory(
typeof(DockPanel)));
using (var db = new WorkShopContext())
{
foreach (var m in db.Menus
.Where(p => p.mParentId == 0)
.ToList())
{
// ADD MAIN MENUITEMS
if (!string.IsNullOrEmpty(m.mParentId.ToString()))
{
MenuItem mainItem = new MenuItem();
mainItem.Header = m.mHeader;
mainItem.Tag = m.mTag;
mainItem.Click += MenuItem_Click;
if (m.mHeader == "Login")
{
mainItem.HorizontalAlignment = HorizontalAlignment.Right;
}
mainMenu.Items.Add(mainItem);
}
}
AddChildItems();
}
}
private void AddChildItems()
{
foreach (var c in db.Menus
.Where(p => p.mParentId > 0)
.ToList())
{
if (!string.IsNullOrEmpty(c.mParentId.ToString()))
{
MenuItem childItem = new MenuItem();
childItem.Header = c.mHeader;
childItem.Tag = c.mTag;
childItem.Click += MenuItem_Click;
mainMenu.Items.Add(childItem);
}
}
}
---------------------------------------------------------------------------------------
|
|
|
|
|
Try passing the mainmenuitem to the addchilditems and then add the childitem to the mainmenuitems collection of menuitems.
You are currently adding the childitem to the mainmenu
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|