|
 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
|
|
|
|
|
Would it be possible for you to point to some examples on CodeProject or provide a C# example, showing me what I would need to do, to correct the code I currently have so far. Any help would be greatly appropriated.
|
|
|
|
|
Scratching my head on this.
I have a list of inventory items, and I want to add how many has sold from orders. I can get the inventory, and then loop that inventory to get the "itemNumber", and query the orders to extract only orders that contain that item. But each order can have multiple items sold, so I need to get the "items sold array" within the order.
This is written for MongoDB, and is not really a MongoDB question. I have no clue how to write this in pure Mongo C# driver.
OrderId<br />
OrderNumber<br />
OrderItems = > ItemId<br />
OrderItems = > ItemNumber<br />
OrderItems = > Qty<br />
OrderItems = > Price
I wrote code to get all my inventory items, and used a for each of each item to query the orders. But I can't figure out the GroupBy part, or perhaps GroupBy is not the right solution.
The var in purple below is suggesting the order model, and not the OrderItems within the Order model. I know I constructed the GroupBy wrong, or using GroupBy is just plain wrong.
var start = (page * show) - show;
var query = _context.MarketPlaceInventory.Find(mp => mp.MarketChannel.Equals(marketChannel)).SortBy(s => s.ItemNumber);
var totalTask = query.CountDocumentsAsync();
var inventoryTask = query.Skip(start).Limit(show).ToListAsync();
await Task.WhenAll(totalTask, inventoryTask);
foreach (var product in inventoryTask.Result)
{
var queryFilter = Builders<MarketPlaceOrders>.Filter.ElemMatch(oi => oi.OrderItems, item => item.ItemNumber == product.ItemNumber);
var ordersTask = _context.MarketPlaceOrders.Find(queryFilter).ToListAsync();
await Task.WhenAll(ordersTask);
var orderItems = ordersTask.Result.GroupBy(x => x.OrderItems).Select(item => new MarketPlaceOrderItems() {
ItemQty = item.Sum(qty => <code>qty.ItemQty</code>)
});
Console.Write(orderItems);
}
return new Get_MarketPlaceInventory
{
Count = totalTask.Result,
Inventory = inventoryTask.Result,
Page = page,
Show = show
};
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Inventory: Item #, Quantity on hand (QTY).
Order Item: Item #, Quantity sold (-QTY).
Merge and sum to get the inventory balance (or stock "not committed").
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
|
|
|
|
|
Hi!
I try to show dynamically a Xamarin CollectionView (as a visual list of multiple notes), by following this tuto: https://medium.com/a-developer-in-making/how-to-work-with-collectionview-in-xamarin-forms-5dc65c50b419[^]
Everything's work fine, until I try to return a list made of data from a Realm database.
So, that code from the tuto is working fine:
return new List<Note>
{
new Note() { Category = listFolders[0].IDCategories, Title = listFolders[0].Title, ContentNote = listFolders[0].ContentNote, Image = listFolders[0].Icon },
new Note() { Category = listFolders[1].IDCategories, Title = listFolders[1].Title, ContentNote = listFolders[1].ContentNote, Image = listFolders[1].Icon },
new Note() { Category = listFolders[2].IDCategories, Title = listFolders[2].Title, ContentNote = listFolders[2].ContentNote, Image = listFolders[2].Icon },
};
And it doesn't work when I try to do the same thing, from a Realm database. Something like that:
(there is no errors, it's just show nothing during the debug).
List<Note> list = new List<Note>();
for (int i = 0; i < countFolders; i++)
{
new Note() { Category = listFolders[i].IDCategories, Title = listFolders[i].Title, ContentNote = listFolders[i].ContentNote, Image = listFolders[i].Icon };
}
return list;
And I tried that too:
return new List<Note>
{
for (int i = 0; i < countFolders; i++)
{
new Note() { Category = listFolders[i].IDCategories, Title = listFolders[i].Title, ContentNote = listFolders[i].ContentNote, Image = listFolders[i].Icon };
}
}
But just after the first bracket, VS Studio say: " } awaited " and " ; awaited ". But I don't know where precisly... And there is no correction suggested.
I'm sure that's probably simple, but as a beginner, I still miss some things.
Thanks!
|
|
|
|
|
Member 14992862 wrote: but with code errors...
We cannot guess what code errors you get or where they occur. Please edit your question and add the full details.
|
|
|
|
|
Because it was just to expose something that I've tried, without thinking especially that the solution is there, but maybe:
return new List<Note>
{
for (int i = 0; i < countFolders; i++)
{
new Note() { Category = listFolders[i].IDCategories, Title = listFolders[i].Title, ContentNote = listFolders[i].ContentNote, Image = listFolders[i].Icon };
}
}
Just after the first bracket, VS Studio say: " } awaited " and " ; awaited ". But I don't know where precisly... And there is no correction suggested.
Thanks!
|
|
|
|
|
You can't write a collection initializer like that: it's a list of properties, but you can't use loops in there at all as it uses the Add method on each instance in the list: Object and Collection Initializers - C# Programming Guide | Microsoft Docs[^]
"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!
|
|
|
|
|
You never actually add the new Note to the List (call it something else eg NoteList)
for...
Note X = new Note()...
NoteList.Add(x)
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Aaah, yes, I got it! Thanks to you both!
So, the solution is:
List<Note> list = new List<Note>();
for (int i = 0; i < countFolders; i++)
{
Note AddAllContent = new Note(list) { Category = listFolders[i].IDCategories, Title = listFolders[i].Title, ContentNote = listFolders[i].ContentNote, Image = listFolders[i].Icon };
list.Add(AddAllContent);
}
return list;
|
|
|
|
|
I'm a C# user. I did some search in Internet but I couldn't find any proper solution for my problem.
I have a DataGridView which adds rows and fill their value only by pressing a button. The input values is achieved from some Textboxes. My last Column is a Dropbox which has 3 defined items. I want to select first Item as a default value when the button is pressed. User can change it manually later.
How can I do this programmatically?
I tried this code for the button (some part of the code):
Int32 LastRow = dataGridView1.Rows.Count - 1;
dataGridView1.Rows[LastRow].Cells[9].Value = new DataGridViewComboBoxCell { };
The code after "=" is wrong. I don'y know what to write inside curley brackets.

|
|
|
|
|