Docx - Removing section of document - c#

Is there a way to remove sections of a document where i can specify the beginning and ending tags?
i need a way that i can remove a section of the document by passing in both my start and end catches, (##DELETEBEGIN and ##DELETEEND)
for example i have this in my document:
Hello, welcome to this document
##DELETEBEGIN{Some values to check in the code}
Some text that will be removed if the value is true
##DELETEEND
Final Line

If you need to delete text from ##DELETEBEGIN to ##DELETEEND, where ##DELETEBEGIN is not at the beginning of a Paragraph and ##DELETEEND is not at the end of a Paragraph, this code should work.
DocX document = DocX.Load("C:\\Users\\phil\\Desktop\\text.docx");
bool flag = false;
List<List<string>> list1 = new List<List<string>>();
List<string> list2 = new List<string>();
foreach (Novacode.Paragraph item in document.Paragraphs)
{
//use this if you need whole text of a paragraph
string paraText = item.Text;
var result = paraText.Split(' ');
int count = 0;
list2 = new List<string>();
//use this if you need word by word
foreach (var data in result)
{
string word = data.ToString();
if (word.Contains("##DELETEBEGIN")) flag = true;
if (word.Contains("##DELETEEND"))
{
flag = false;
list2.Add(word);
}
if (flag) list2.Add(word);
count++;
}
list1.Add(list2);
}
for (int i = 0; i < list1.Count(); i++)
{
string temp = "";
for (int y = 0; y < list1[i].Count(); y++)
{
if (y == 0)
{
temp = list1[i][y];
continue;
}
temp += " " + list1[i][y];
}
if (!temp.Equals("")) document.ReplaceText(temp, "");
}
document.Save();
I have to give some credit to this post for looping through each word.

I think i have found a solution to this, at least it works for me, please let me know if there is anything i can do better:
the deleteCommand would be the ##DELETEBEGIN string and the deleteEndCommand would be the ##DELETEEND
private void RemoveSection(DocX doc, string deleteCommand, string deleteEndCommand)
{
try
{
int deleteStart = 0;
int deleteEnd = 0;
//Get the array of the paragraphs containing the start and end catches
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
if (doc.Paragraphs[i].Text.Contains(deleteCommand))
deleteStart = i;
if (doc.Paragraphs[i].Text.Contains(deleteEndCommand))
deleteEnd = i;
}
if (deleteStart > 0 && deleteEnd > 0)
{
//delete from the paraIndex as the arrays will shift when a paragraph is deleted
int paraIndex = deleteStart;
for (int i = deleteStart; i <= deleteEnd; i++)
{
doc.RemoveParagraphAt(paraIndex);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Related

I'm making a guessing game but the random word is always the same

I have 100 random words in a notepad file & choosing a random word but it's always choosing the same word which is the last one in the file every word is a line.
Here is the code for taking a random word & showing it as:
private string[] tab;
private string current = "";
private string copycurrent = "";
private void randomword()
{
string line = "";
try
{
using (StreamReader sr = new StreamReader("Words.txt"))
{
string[] tab = null;
while ((line = sr.ReadLine()) != null)
{
tab = line.Split("\n");
}
Random r = new Random();
int j = r.Next(tab.Length);
current = tab[j];
copycurrent = "";
for (int i = 0; i < current.Length; i++)
{
copycurrent += "_";
}
label1.Text = "";
for (int i = 0; i < copycurrent.Length; i++)
{
label1.Text += copycurrent.Substring(i, 1);
label1.Text += " ";
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Please - can anyone help? I would really appreciate it! Thanks in advance.
I don't know why it doesn't work like I've been quite wondering for some time. BTW I'm making a hangman game if anyone want the project hmu ig: rami_chalouhi. its like an actual game with login and loading etc..
You keep overwriting 'tab'. you only have the last line
string[] tab = null;
while ((line = sr.ReadLine()) != null)
{
tab = line.Split("\n");
}
Better would be a list of words
var words = new List<string>();
while ((line = sr.ReadLine()) != null)
{
var tab = line.Split("\n");
words.AddRange(tab);
}
in fact that split doesnt make any sense, ReadLine will already be splitting on new line (my first scan I assumed you were splitting on a needed delimter like space or ',') So all that code can be replaced by
var words = File.ReadAllLines("Words.txt");
see https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readalllines?view=net-6.0
The rest of that code is pretty odd too
current = tab[j];
copycurrent = "";
for (int i = 0; i < current.Length; i++)
{
copycurrent += "_";
}
label1.Text = "";
for (int i = 0; i < copycurrent.Length; i++)
{
label1.Text += copycurrent.Substring(i, 1);
label1.Text += " ";
}
you want
a string of "_" thats the same length as the selected word
to fill label1.Text with that _ string plus an extra space
current = tab[j];
var copycurrent = new string('_', current.Length);
label1.Text = copycurrent + " ";
is a lot simpler and clearer
So that whole function becomes
string current;
string copycurrent;
try
{
var words = File.ReadAllLines("Words.txt");
Random r = new Random();
int j = r.Next(words.Length);
current = words[j];
copycurrent = new string('_', current.Length);
label1.Text = copycurrent + " ";
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

How can I use indexof and substring to find words in a string?

In the constructor :
var tempFR = File.ReadAllText(file);
GetResults(tempFR);
Then :
private List<string> GetResults(string file)
{
List<string> results = new List<string>();
string word = textBox1.Text;
string[] words = word.Split(new string[] { ",," }, StringSplitOptions.None);
for(int i = 0; i < words.Length; i++)
{
int start = file.IndexOf(words[i], 0);
results.Add(file.Substring(start));
}
return results;
}
words contains in this case 3 words System , public , test
I want to find all the words in file and add them to the list results using indexof and substring.
The way it is now start value is -1 all the time.
To clear some things.
This is a screenshot of the textBox1 :
That is why I'm using two commas to split and get the words.
This screenshot showing the words after split them from the textBox1 :
And this is the file string content :
I want to add to the List results all the words in the file.
When looking at the last screenshot there should be 11 results.
Three time the word using three times the word system five times the word public.
but the variable start is -1
Update :
Tried Barns solution/s but for me it's not working good.
First the code that make a search and then loop over the files and reporting to backgroundworker :
int numberofdirs = 0;
void DirSearch(string rootDirectory, string filesExtension, string[] textToSearch, BackgroundWorker worker, DoWorkEventArgs e)
{
List<string> filePathList = new List<string>();
int numberoffiles = 0;
try
{
filePathList = SearchAccessibleFilesNoDistinct(rootDirectory, null, worker, e).ToList();
}
catch (Exception err)
{
}
label21.Invoke((MethodInvoker)delegate
{
label21.Text = "Phase 2: Searching in files";
});
MyProgress myp = new MyProgress();
myp.Report4 = filePathList.Count.ToString();
foreach (string file in filePathList)
{
try
{
var tempFR = File.ReadAllText(file);
_busy.WaitOne();
if (worker.CancellationPending == true)
{
e.Cancel = true;
return;
}
bool reportedFile = false;
for (int i = 0; i < textToSearch.Length; i++)
{
if (tempFR.IndexOf(textToSearch[i], StringComparison.InvariantCultureIgnoreCase) >= 0)
{
if (!reportedFile)
{
numberoffiles++;
myp.Report1 = file;
myp.Report2 = numberoffiles.ToString();
myp.Report3 = textToSearch[i];
myp.Report5 = FindWordsWithtRegex(tempFR, textToSearch);
backgroundWorker1.ReportProgress(0, myp);
reportedFile = true;
}
}
}
numberofdirs++;
label1.Invoke((MethodInvoker)delegate
{
label1.Text = string.Format("{0}/{1}", numberofdirs, myp.Report4);
label1.Visible = true;
});
}
catch (Exception err)
{
}
}
}
I have the words array already in textToSearch and the file content in tempFR then I'm using the first solution of Barns :
private List<string> FindWordsWithtRegex(string filecontent, string[] words)
{
var res = new List<string>();
foreach (var word in words)
{
Regex reg = new Regex(word);
var c = reg.Matches(filecontent);
int k = 0;
foreach (var g in c)
{
Console.WriteLine(g.ToString());
res.Add(g + ":" + k++);
}
}
Console.WriteLine("Results of FindWordsWithtRegex");
res.ForEach(f => Console.WriteLine(f));
Console.WriteLine();
return res;
}
But the results I'm getting in the List res is not the same output in Barns solution/s this is the results I'm getting the List res for the first file :
In this case two words system and using but it found only the using 3 times but there is also system 3 times in the file content. and the output format is not the same as in the Barns solutions :
Here is an alternative using Regex instead of using IndexOf. Note I have created my own string to parse, so my results will be a bit different.
EDIT
private List<string> FindWordsWithCountRegex(string filecontent, string[] words)
{
var res = new List<string>();
foreach (var word in words)
{
Regex reg = new Regex(word, RegexOptions.IgnoreCase);
var c = reg.Matches(filecontent).Count();
res.Add(word + ":" + c);
}
return res;
}
Simple change this part and use a single char typically a space not a comma:
string[] words = word.Split(' ');
int start = file.IndexOf(words[i],0);
start will be -1 if the word is not found.
MSDN: IndexOf(String, Int32)
for(int i = 0; i < words.Length; i++)
{
int start = file.IndexOf(words[i], 0);
// only add to results if word is found (index >= 0)
if (start >= 0) results.Add(file.Substring(start));
}
If you want all appearance of the words you need an extra loop
int fileLength = file.Length;
for(int i = 0; i < words.Length; i++)
{
int startIdx = 0;
while (startIdx < fileLength ){
int idx = file.IndexOf(words[i], startIdx]);
if (start >= 0) {
// add to results
results.Add(file.Substring(start));
// and let Word-search continue from last found Word Position Ending
startIdx = (start + words.Length);
}
}
int start = file.IndexOf(words[i], 0);
// only add to results if word is found (index >= 0)
if (start >= 0) results.Add(file.Substring(start));
}
MayBe you want a caseinsensitiv search
file.IndexOf(words[i], 0, StringComparison.CurrentCultureIgnoreCase); MSDN: StringComparer Class

Reading CSV file and converted to transposed table

I need to read from a CSV file (separated via “;”) and a new file should be created containing the transposed (rotated) table:
my input file:
The tool shall be able to receive the filename of the table from the user and load the table to transpose the content.
The tool shall be able to save the transposed table in a new file with the filename of the input file and extended with “transposed” (“filename_transposed.csv”).
my Code
public void ReadCsv()
{
// open the file "data.csv" which is a CSV file with headers
using (CsvReader csv = new CsvReader(
new StreamReader("C:\\Users\\moki\\Downloads\\Input.csv"), true))
{
int fieldCount = csv.FieldCount;
string[] headers = csv.GetFieldHeaders();
while (csv.ReadNextRecord())
{
for (int i = 0; i < fieldCount; i++)
Console.WriteLine(string.Format("{0}\n{1}",
headers[0], csv[i]) );
}
}
Console.ReadLine();
my Result
Since we're providing answers...
using System;
using System.Collections.Generic;
using System.Linq;
namespace _51306985
{
class Program
{
static List<List<string>> listOfList = new List<List<string>>();
static int longestCol = 0;
static void Main(string[] args)
{
FillTheList("M:\\StackOverflowQuestionsAndAnswers\\51306985\\testdata.csv");
PadTheList();
SpitItBackOut();
SpitItOutToAFile("M:\\StackOverflowQuestionsAndAnswers\\51306985\\testdata.csv");
Console.ReadLine();
}
private static void SpitItOutToAFile(string v)
{
string newPath = $"{System.IO.Path.GetDirectoryName(v)}\\{System.IO.Path.GetFileNameWithoutExtension(v)}_Rotated{System.IO.Path.GetExtension(v)}";
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(newPath))
{
for (int i = 0; i < longestCol; i++)
{
string lineToWrite = string.Empty;
for (int b = 0; b < listOfList.Count; b++)
{
lineToWrite += $"{listOfList[b][i]},";
}
lineToWrite = lineToWrite.Substring(0, lineToWrite.Length - 1);//remove the hanging comma
if (lineToWrite != "")
{
sw.WriteLine(lineToWrite);
}
}
}
}
private static void SpitItBackOut()
{
for (int i = 0; i < longestCol; i++)
{
string lineToWrite = string.Empty;
for (int b = 0; b < listOfList.Count; b++)
{
lineToWrite += $"{listOfList[b][i]},";
}
lineToWrite = lineToWrite.Substring(0, lineToWrite.Length - 1);//remove the hanging comma
if (lineToWrite != "")
{
Console.WriteLine(lineToWrite);
}
}
}
private static void PadTheList()
{
foreach (List<string> item in listOfList)
{
while (item.Count < longestCol)
{
item.Add("");
}
}
}
private static void FillTheList(string v)
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(v))
{
string currentLine = string.Empty;
while ((currentLine = sr.ReadLine()) != null)
{
listOfList.Add(currentLine.Split(',').ToList());
if (listOfList.Last().Count > longestCol)
{
longestCol = listOfList.Last().Count;
}
}
}
}
}
}
Input Data
a1,b1,c1,d1,e1
a2,b2,c2,d2,e2
a3,b3,c3,d3,e3
a4,b4,c4,d4,e4
a5,b5,c5,d5,e5
a6,b6
a7,b7,c7,d7
a8,b8,c8
Output
a1,a2,a3,a4,a5,a6,a7,a8
b1,b2,b3,b4,b5,b6,b7,b8
c1,c2,c3,c4,c5,,c7,c8
d1,d2,d3,d4,d5,,d7,
e1,e2,e3,e4,e5,,,
There surely is a more efficient way but this is a easy to understand way I think:
1.) put the data into a datatable, e.g. like:
StreamReader sr1 = new StreamReader("C:\\Users\\moki\\Downloads\\Input.csv"); //create the streamreader to read the input .csv
DataTable mydata = new DataTable(); //create an empty DataTable.....
string[] arr; //....and an array in which you will store the elemnets of each line
int i = 0; //just a variable to help counting where you are in your data
bool mydatasetup = false; //a variable to check in the loop if you already added the necessary number of columns to the datatable
using (sr1)
{
while (sr1.EndOfStream == false) //read the whole file
{
string line = sr1.ReadLine(); //get a line from the file
if (line != null && line != String.Empty) //check if there is content in the line
{
arr = line.Split(';'); //split the line at each ";" and put the elements in the array
if(mydatasetup == false) //after reading the first line add as many columns to your datatable as you will need.....
{
for (int u = 0; u < arr.Length; u++)
{
mydata.Columns.Add();
}
mydatasetup = true; //...but only do this once (otherwise you wil have an unneccessary big datatable
}
mydata.Rows.Add(); //add a row in you datatable in which you will store the data of the line
for (int j = 0; j < arr.Length; j++) //go throught each element in your array and put it into your datatable
{
if (arr[j] != "")
{
mydata.Rows[i][j] = arr[j];
}
}
i = i + 1; //increase the counter so that the program knows it has to fill the data from the next line into the next row of the datatable
}
}
}
2.) Then you can loop through your datatable's columns and add each row's contents to a Stringbuilder (whereby you transpose your data) which you then save as a .csv:
StringBuilder sb = new StringBuilder(); //create a stringbuilder
for (int u = 0; u < mydata.Columns.Count; u++) //loop through the COLUMNS of your datatable....
{
for (int i = 0; i < mydata.Rows.Count; i++) //....but for each column go through each row in the datatable first
{
sb.Append(mydata.Rows[i][u].ToString()); // and add the elements to the stringbuilder - here the transposing is actually done
if (i < mydata.Rows.Count - 1) //add a deliminator after each element because you want a .csv as output again
{
sb.Append(';');
}
}
sb.AppendLine(); //add another line to your stringbuilder in which you will store the next column of your datatable
}
File.WriteAllText("C:\\Users\\moki\\Downloads\\Output.csv", sb.ToString()); //finally create the output .csv
You could of course combine these two steps.
Just in case, if anyone want to know how to do it using Cinchoo ETL with few lines of code,
string csv = #"A1;B1;C1;D1;E1
A2;B2;C2;D2;E2
A3;B3;C3;D3;E3
A4;B4;C4;D4;E4
A5;B5;C5;D5;E5
";
StringBuilder sb = new StringBuilder();
using (var p = ChoCSVReader.LoadText(csv)
.WithDelimiter(";")
.ThrowAndStopOnMissingField(false)
)
{
using (var w = new ChoCSVWriter(sb)
.WithDelimiter(";")
)
{
w.Write(p.Cast<ChoDynamicObject>().Transpose(false));
}
}
Console.WriteLine(sb.ToString());
Output:
A1;A2;A3;A4;A5
B1;B2;B3;B4;B5
C1;C2;C3;C4;C5
D1;D2;D3;D4;D5
E1;E2;E3;E4;E5

How to retrieve text commented or bookmarked in Microsoft Office Word, with OpenXML

I've been trying for a while to retrieve text that is commented or bookmarked by the user, in the .docx document, using OpenXML. I tried building dictionaries and arrays with the start and end tag of each comments/bookmarks, and tried to navigate through the XML tree nodes, to obtain the text but I'm not obtaining all of it (just the first child, which is the first word).
IDictionary<String, BookmarkStart> bookmarkMapS = new Dictionary<String, BookmarkStart>();
IDictionary<String, BookmarkEnd> bookmarkMapE = new Dictionary<String, BookmarkEnd>();
var _bkms = doc.MainDocumentPart.RootElement.Descendants<BookmarkStart>();
var _bkme = doc.MainDocumentPart.RootElement.Descendants<BookmarkEnd>();
foreach (BookmarkStart item in _bkms)
{
Run bookmarkText = item.NextSibling<Run>();
if (bookmarkText != null)
{
try
{
for (int i = 0; i < bookmarkText.ChildElements.Count(); i++)
{
Console.WriteLine(bookmarkText.ChildElements.ElementAt(i).InnerText);
}
}
catch (Exception)
{
}
}
}
There is probably a better way to do this, but this is what I came up with.
List<OpenXmlElement> elems =new List<OpenXmlElement>(doc.MainDocumentPart.Document.Body.Descendants());
var crs=doc.MainDocumentPart.RootElement.Descendants<CommentRangeStart>();
var cre=doc.MainDocumentPart.RootElement.Descendants<CommentRangeEnd>();
var dic_cr=new Dictionary<CommentRangeStart, CommentRangeEnd>();
for (int i = 0; i < crs.Count(); i++)
{
dic_cr.Add(crs.ElementAt(i), cre.ElementAt(i));
}
for (int i = 0; i < elems.Count; i++)
if (elems.ElementAt(i).LocalName.Equals("t"))
if (isInsideAnyComment(dic_cr, elems.ElementAt(i)))
for (int j = 0; j < dic_cr.Count; j++)
if (isInsideAComment(dic_cr.ElementAt(j), elems.ElementAt(i)))
String text = ((Text)elems.ElementAt(i)).InnerText;
public bool isInsideAnyComment(IDictionary<CommentRangeStart, CommentRangeEnd> dic_cr, OpenXmlElement item)
{
foreach (var i in dic_cr)
if (item.IsAfter(i.Key) && item.IsBefore(i.Value))
return true;
return false;
}
public bool isInsideAComment(KeyValuePair<CommentRangeStart, CommentRangeEnd> dic_cr_elem, OpenXmlElement item)
{
if (item.IsAfter(dic_cr_elem.Key) && item.IsBefore(dic_cr_elem.Value))
return true;
return false;
}

c# Add Specific columns from a TEXT file to DataGridView

Hello Everyone,
As shown in the above image I want to add the decimal numbers column wise from a text file to datagrid control.
Following is my code snippet
List<string> str = new List<string>();
String st = "";
int k = 0;
string[] s ;
//Path to write contents to text file
string filename = #"E:\Vivek\contentcopy\clientlist.txt";
Form.CheckForIllegalCrossThreadCalls = false;
OpenFileDialog ofd = new OpenFileDialog();
ofd.FileName = "";
ofd.ShowDialog();
st = ofd.FileName;
if (string.IsNullOrEmpty(ofd.FileName))
return;
string Name = "", No1 = "",No2="";
string[] lines = File.ReadAllLines(st).Where(sw => !string.IsNullOrWhiteSpace(sw)).ToArray();
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Contains("VENTURA SECURITIES LIMITED (NSE F&O)")) continue;
if (lines[i].Contains("ALL EXCHANGES DERIVATIVES CLIENTWISE STATEMENT AS ON 16-05-2012")) continue;
if (lines[i].Contains("-------------------------------------------------------")) continue;
s = lines[i].Split(' ');
if (s[0] == "PARTY" || s[0] == "") continue;
int z;
Name = "";
for (z = 1; z < s.Length; z++)
{
if (s[z] == "") continue;
if (s[z].Contains('.'))
{
No1+=s[z]+" ";
No2 = No1 + " ";
}
else
{
Name += s[z];
str.Add(s[0]+" "+Name);
}
}
dataGridView1.Rows.Add();
dataGridView1.Rows[k].Cells[0].Value = s[0];
dataGridView1.Rows[k].Cells[1].Value = Name;
dataGridView1.Rows[k].Cells[2].Value = No1;
dataGridView1.Rows[k].Cells[3].Value = No2;
k++;
}
File.WriteAllLines(filename, str);
dataGridView1.ReadOnly = true;
}
The line No1=s[z] directly takes the last column values ie 46,123.19 and so on.I want to fetch each column from the text file and store it in a string variable and then assign it to the datagrid view
I hope my doubt is clear.If not please let me know
Here is the simplest Solution:
Add a DataGrid View to Form and add a Button:
private void button1_Click(object sender, EventArgs e)
{
ReadAndFileter();
}
private void ReadAndFileter()
{
try
{
using(System.IO.StreamReader reader = new System.IO.StreamReader("file.txt"))
{
string line;
string []array;
int rowcount= 0;
decimal number;
string[] separators = { "\t", " " };
int columnCount = 0;
while ((line = reader.ReadLine()) != null)
{
array = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);
dataGridView1.Rows.Add();
foreach (string str in array)
{
if (Decimal.TryParse(str,out number))
{
dataGridView1.Rows[rowcount].Cells[columnCount++].Value = number;
}
}
rowcount++;
columnCount = 0;
}
}
}
catch (Exception ex)
{
}
}
The File Contents are:
Abc 20.122 69.33 0.00 693.25 0.00
def 36.20 96.20 1.15 69.56 8.96
And the final output:
Lets say, you have for lines in your test file, then u need to do following things:
Use StreamReader.ReadLine(), to read one line at time.
Spilt the line using split(' ') and store it in a array
Remove all the empty ones from the array
Now at index 2,3,4,5,6 of the resulting array will have the string equivalent of the decimal numbers.
Repeat this for each StreamReader.ReadLine()
Hope this will help.
Your problem is that you are overwriting No1 every time you read a string, which explains why you only get the last value. What you could do is either;
Append the string:
No1 += s[z] + " ";
Which will put all the values behind eachother, seperated by a whitespace.
Or, you could make a List<String> and add each value to the list, meaning you have them stored seperated:
List<String> values = new List<String>();
foreach(...)
{
if (s[z] == "") continue;
if (s[z].Contains('.'))
{
values.Add(s[z])
}
else
{
Name += s[z];
str.Add(s[0] + " " + Name);
}
}
You can thereafter loop through the list and add each value to a row. Considering your code piece;
int i = 2;
foreach(string value in values)
{
dataGridView1.Rows[k].Cells[i].Value = value;
i++;
}
This should work.
Hope this helps.
Here is edited code: but for future I must suggest to give a try at least..
private void ReadAndFileter1()
{
try
{
using (System.IO.StreamReader reader = new System.IO.StreamReader("file.txt"))
{
string line;
string[] array;
int rowcount = 0;
decimal number;
string[] separators = { "\t", " " };
int columnCount = 1;
string[] lines = File.ReadAllLines("file.txt");
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Contains("VENTURA SECURITIES LIMITED (NSE F&O)")) continue;
if (lines[i].Contains("ALL EXCHANGES DERIVATIVES CLIENTWISE STATEMENT AS ON 16-05-2012")) continue;
if (lines[i].Contains("-------------------------------------------------------")) continue;
array = lines[i].Split(separators, StringSplitOptions.RemoveEmptyEntries);
if (array[0] == "PARTY" || array[0] == "") continue;
dataGridView1.Rows.Add();
foreach (string str in array)
{
if (Decimal.TryParse(str, out number))
{
dataGridView1.Rows[rowcount].Cells[columnCount++].Value = number;
}
}
dataGridView1.Rows[rowcount].Cells[0].Value = array[0];
rowcount++;
columnCount = 1;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Here it is:
static void Main(string[] args)
{
Decimal result;
string[] splitchar = new string[]{" "};
using(StreamReader reader = new StreamReader(#"C:\Users\Dell\Desktop\input.txt"))
{
while(!reader.EndOfStream)
{
string[] splittedArray = reader.ReadLine().Split(splitchar, StringSplitOptions.RemoveEmptyEntries).Where(x => Decimal.TryParse(x, out result)).ToArray();
// put your code here to get insert the values in datagrid
}
}
}

Categories

Resources