I have this code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TabeleExcel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cboSheet_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = tableCollection[cboSheet.SelectedItem.ToString()];
dataGridView1.DataSource = dt;
}
DataTableCollection tableCollection;
private void btnBrowse_Click(object sender, EventArgs e)
{
using(OpenFileDialog openFileDialog=new OpenFileDialog() { Filter="Excel|*.xls|Excel|*.xlsx" })
{
if(openFileDialog.ShowDialog()==DialogResult.OK)
{
txtFilename.Text = openFileDialog.FileName;
using(var stream=File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
{
using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
{
DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable=(_)=>new ExcelDataTableConfiguration() { UseHeaderRow=true }
});
tableCollection = result.Tables;
cboSheet.Items.Clear();
foreach (DataTable table in tableCollection)
cboSheet.Items.Add(table.TableName);
}
}
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
The code makes a GUI in .NET framework and it lets you upload an .xls or .xlsx, the Excel file will be shown as a DataGrid view. I implemented a new textbox ( the textBox1_TextChanged(object sender, EventArgs e) method), and based on the user's input, I want to it to display only the rows that correspond with the input. How am I supposed to do that? Thanks in advance!
You have 3 options for working with office formats:
if you only need to support the new formats (.xslx) you can use the OpenXML SDK, any of the wrappers people wrote for it. Or even the ZipArchive and XMLReader classes. It is well known and easily processed
If you need to support the old format as well (.xls) you have to use the (t)rusty Office COM interop. That one has the usualy issues of COM interop, plus some unique problems. Like requiring a Interactive Session due to poor design choices.
For any given problem, any given file format, any given GUI technology there might be another way. But those are far and few in between
My standing advice is to use the OpenXML way and learn to live without the old formats altogether. They are way more trouble to support then it is worth.
Related
Just recently began tinkering with Awesomium, it's very cool and much better than the stock webBrowser for WinForms.
However, when I use the _LoadingFrameComplete method to determine if the page has loaded, it seems to be firing 10+ times (when used on Facebook, 2 times when navigating to google.com)
I am trying to get the comparable method of webBrowser1_DocumentCompleted (which only fires one time, after the document has completed).
Is this a 'me' problem, or am I using the wrong methods to check whether the website has finished loading completely.
I'm using Visual C# 2010 Edition
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Debugging_Problems
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string searchURL = textBox1.Text;
webControl1.Source = new Uri(searchURL);
}
private void Awesomium_Windows_Forms_WebControl_LoadingFrameComplete(object sender, Awesomium.Core.FrameEventArgs e)
{
richTextBox1.AppendText("Completed.\n");
}
}
}
You need to use IsMainFrame
private void Awesomium_Windows_Forms_WebControl_LoadingFrameComplete(object sender, Awesomium.Core.FrameEventArgs e)
{
if (e.IsMainFrame)
{
richTextBox1.AppendText("Completed.\n");
}
}
Try putting if(e.IsMainFrame) { .... } inside your LoadingFrameComplete event handler and only put your code in there. – Jon
That was the answer. Thank you.
I want to create a simple database viewer using Microsoft Visual Studio and GMaps.NET which can (1) do a specific search and update the list, (2) export the list as KML, and (3) upload the KML file to be viewed in the embedded gmap. Step 1 is already resolved but I cannot find reference to proceed with steps 2 and 3. Any help will be highly appreciated!
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using GMap.NET.MapProviders;
//if these GMap statements are not enabled, these will have to be typed with the namespaces e.g. GMap.Net.MapProviders.GoogleMap
namespace KDMI_DBViewer_v1._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void existingPermitsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.existingPermitsBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.permits_2016DataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'permits_2016DataSet.ExistingPermits' table. You can move, or remove it, as needed.
this.existingPermitsTableAdapter.Fill(this.permits_2016DataSet.ExistingPermits);
}
private void gMapControl1_Load(object sender, EventArgs e)
{
gMapControl1.MapProvider = GMapProviders.GoogleTerrainMap;
//gMapControl1.ShowCenter = false; if enabled, this will remove the red cross
gMapControl1.Position = new PointLatLng(12.0725555, 122.895494);
gMapControl1.DragButton = MouseButtons.Left;
gMapControl1.Zoom = 5;
gMapControl1.MinZoom = 5;
gMapControl1.MaxZoom = 12;
}
private void specificSearchToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.existingPermitsTableAdapter.SpecificSearch(this.permits_2016DataSet.ExistingPermits, provinceToolStripTextBox.Text, municipalityToolStripTextBox.Text, typeToolStripTextBox.Text);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
Unfortunetly, no KML parser yet implemented as the creator of GMap.Net said gmap.net issues.
But instead you can create the import \ export KML\KMZ methods yourself using a library called SharpKML
SharpKML is an implementation of the Open Geospatial Consortium (OGC)
KML 2.2 standard developed in C#, able to read/write both KML files
and KMZ files.
Getting Started:
The easiest way to use the library is to install the SharpKml.Core
NuGet package, however, you can also download the binaries/source code
from this project page.
I'm trying to import CSV and display the data in DataGridView by using the way that i can understand.
Here is the code and the explanation that i understand how it works so far.
Please do correct me if i miss understand.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test2
{
public partial class Form1 : Form
{
//Open the choose GUI to choose the file that we want to import
OpenFileDialog openFile = new OpenFileDialog();
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
//if open successfully, then apply streamReader to it
if (openFile.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFile.FileName);
//read the data in the file by using readLine
var rl = sr.ReadLine();
// If the rl is not null, then print (is it correct?)....
if(rl != null)
{
///code to print data
}
}
}
//filter out the csv file.
private void Form1_Load_1(object sender, EventArgs e)
{
openFile.Filter = "CSV|*.csv";
}
}
}
Now, i'm trying to print the data.
I know i need to use DataGridView.DataSource to print the data(correct me if i'm wrong) but i have no idea how to apply.
So, is my explain is true or is there anything i have add???
--beginner.
Sample data image.
You can build a List (of string, or whichever data type you want) and use it as DataSource. Assuming the x:name of your DataGridView is dgv.
private void Button1_Click(object sender, EventArgs e)
{
//if open successfully, then apply streamReader to it
if (openFile.ShowDialog() == DialogResult.OK)
{
List<string[]> rows = System.IO.File.ReadAllLines(openFile.FileName).Select(x => x.Split(',')).ToList();
System.Data.DataTable dt = new System.Data.DataTable();
List<string> headerNames = rows[0].ToList();
foreach (var header in headerNames)
{
dt.Columns.Add(headers);
}
foreach (var x in rows.Skip(1))
{
if (x.SequenceEqual(headerNames)) //linq to check if 2 lists are have the same elements (perfect for strings)
continue; //skip the row with repeated headers
dt.Rows.Add(x);
}
dgv.DataSource = dt;
}
}
Remember to add using System.Linq;.
Do ask if any part is unclear. You can add more checking to the 2nd foreach loop (e.g. check if all the items in the row are empty).
My answer is based on: Faster way of reading csv to grid
For best way of checking 2 lists equality:
Check if two lists are equal
Good morning guys I'm new to this place and have been having a lot of issues getting any of the findings to work.
I'm currently using C# inside visual studio community and was using a you tube video as reference but I'm stuck getting it to work efficiently (i think its in a loop) and to do other actions.
Im trying to get a list of urls from a text file in desktop or by user inputting it to a field:
EG: program opens HE puts IP in one text box and hits submit program runs and iots done.
or if it pulls a list of IP's from Desktop C: then it will add it to the website while using a loop?
eg:
my current code is similar to this as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HTTPcollect_Windows_Form_App
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> IPs = new List<string>();
WebClient web = new WebClient();
String html = web.DownloadString("http://www.ipvoid.com/scan/8.8.8.8/");
MatchCollection m1 = Regex.Matches(html, #"IP Address.*<strong>(.+?) <\/strong>", RegexOptions.Singleline);
foreach (Match m in m1)
{
string IP = m.Groups[1].Value;
IPs.Add(IP);
}
listBox1.DataSource = IPs;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
New to C# from vb.net and I am just making some mock bound applications for now. I have problems with the following code. If I pic an image and exit the application, there is no change. Even if I move a row. However if I upload an image, move to another row, then add another image. After exiting the application the first image will be there but not the second.
In short I have to attempt to upload to another record before the record I actually want updating will do so.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace DBUserManagement
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dsUsers.Users' table. You can move, or remove it, as needed.
this.usersTableAdapter.Fill(this.dsUsers.Users);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (DialogResult.OK == ofd.ShowDialog())
{
imgUser.SizeMode = PictureBoxSizeMode.StretchImage;
imgUser.Image = new Bitmap(ofd.OpenFile());
//update bound field.
usersTableAdapter.Update(dsUsers);
}
}
}
}
Any ideas on what I am missing or not understand correctly? Any help appreciated.
/P
The answer was I needed to call the BindingSource's .EndEdit(); method.
So I am guessing it was down to the binding source still having hold of something.
Seems like I'm on the right track anyhow, I looked up the details on MSDN :)