Adding an Items into Listview - c#

I have completely split the string in text file that is delimited by a comma.
My problem is that I cannot show the data on my list view.
I have use this code, but when I run it and debug, there is value inside the variables. but then I finished the debugging, No Items were added in the Listview.
private void ColumnHeaders()
{
lvResult.View = View.Details;
lvResult.Columns.Add("ファイル名");
lvResult.Columns.Add("フォルダ");
lvResult.Columns.Add("比較結果");
lvResult.Columns.Add("左日付");
lvResult.Columns.Add("右日付");
lvResult.Columns.Add("拡張子");
for (int i = 0; i <= lvResult.Columns.Count; i++)
{
lvResult.Columns[i].Width = lvResult.Width / 6;
}
}
private void viewTextFile()
{
string[] lines = File.ReadAllLines(txtResultPath.Text + "A.YMD6063_new.txt");
for (int x = 0 ; x <= lines.Length; x++)
{
string[] col = lines[x].Split(new char[] { ',' });
ListViewItem lvItem = new ListViewItem();
for (int i = 0; i <= col.Length; i++)
{
lvItem.Text = col[i].ToString();
if (i == 0)
{
lvResult.Items.Add(lvItem);
}
else
{
lvResult.Items[x].SubItems[i].Text = lvItem.Text;
}
}
}
}

here is a sample code I tried. Hope this would help you.
listView1.Columns.Add("column1");
listView1.Columns.Add("column2");
listView1.Columns.Add("column3");
listView1.Columns.Add("column4");
string[] lines = new string[] { "value01,value02,value03,value04", "value11,value12,value13,value14" };
foreach (string line in lines)
{
listView1.Items.Add(new ListViewItem(line.Split(',')));
}

Set the ListView.View to Details. This can either be achieved in the Designer or programatically like this:
lvResult.View = View.Details;
Add each line of your file:
private void viewTextFile()
{
foreach(var line in File.ReadAllLines(somefilepath))
AddLineToListView(line);
}
private void AddLineToListView(string line)
{
if (string.IsNullOrEmpty(line))
return;
var lvItem = new ListViewItem(line.Split(','));
lvResult.Items.Add(lvItem);
}

Related

Avoid Pasting excel data into a DataGridView header

I need to paste excel data to my data grid for which i found the below code , however the header of my data grid view is being populated with the first row of the pasted data(as per the below screenshot).
I searched for the similar problems here but none of them are solving this .Kindly help me how to solve this.
the copied data should not be pasted for column headers , the paste range should only be restricted to two columns.
code:
private void Btnlotpaste_Click(object sender, EventArgs e)
{
DataObject o = (DataObject)Clipboard.GetDataObject();
if (o.GetDataPresent(DataFormats.Text))
{
if (dataGridView2.RowCount > 0)
dataGridView2.Rows.Clear();
if (dataGridView2.ColumnCount > 0)
dataGridView2.Columns.Clear();
bool columnsAdded = false;
string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
int j = 0;
foreach (string pastedRow in pastedRows)
{
string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });
if (!columnsAdded)
{
for (int i = 0; i < pastedRowCells.Length; i++)
dataGridView2.Columns.Add("col" + i, pastedRowCells[i]);
columnsAdded = true;
j++;
continue;
}
dataGridView2.Rows.Add();
int myRowIndex = dataGridView2.Rows.Count - 1;
using (DataGridViewRow myDataGridViewRow = dataGridView2.Rows[j])
{
for (int i = 0; i < pastedRowCells.Length; i++)
myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
}
j++;
}
}
}
after removing dataGridView2.Rows.Clear();
So it may be like this:
private void Btnlotpaste_Click(object sender, EventArgs e)
{
DataObject o = (DataObject)Clipboard.GetDataObject();
if (o.GetDataPresent(DataFormats.Text))
{
bool columnsAdded = false;
string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
int j = 0;
foreach (string pastedRow in pastedRows)
{
string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });
dataGridView2.Rows.Add();
int myRowIndex = dataGridView2.Rows.Count - 1;
using (DataGridViewRow myDataGridViewRow = dataGridView2.Rows[j])
{
for (int i = 0; i < pastedRowCells.Length; i++)
myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
}
j++;
}
}
}

ListView SelectedIndexChanged items getting inserted twice

I have a list of clauses in my database and a listview in my custom pane in VSTO.
When I select or drag and drop a list item, it gets copied twice. I found this answer.
Listview ItemSelectionChanged fires twice?
But I dont have a isSelected method for my EventArgs e.
Below is my code, please help me out, I want the data/text to be copied only once.
private void clauseList_SelectedIndexChanged(object sender, EventArgs e)
{
ListView.SelectedListViewItemCollection col = clauseList.SelectedItems;
string temp = clauseList.FocusedItem.Text;
clauseList.DoDragDrop(temp, DragDropEffects.Move);
Microsoft.Office.Interop.Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;
Microsoft.Office.Interop.Word.Range range = currentSelection.Range;
currentSelection.TypeText(temp);
currentSelection.TypeParagraph();
clslst(select);
}
public void clslst(object s)
{
DataAccess data = new DataAccess();
List<ClauseDetails> details = new List<ClauseDetails>();
details.AddRange(data.ClausesRelated(s));
string FromtextFromDoc;
FromtextFromDoc = Globals.ThisAddIn.Application.Selection.Text;
Microsoft.Office.Interop.Word.Document docs = Globals.ThisAddIn.Application.ActiveDocument;
List<string> clslist = new List<string>();
string aa = "";
int f = 0;
string tempindex = "";
foreach (Paragraph paragraph in docs.Paragraphs)
{
Style style = paragraph.get_Style() as Style;
string styleName = style.NameLocal;
//var sty = style.Font;
//fontstyle = sty.Name;
//fontsize = sty.Size;
if (styleName.Equals("Heading 1"))
{
f += 1;
if (f == 2)
{
tempindex = aa;
f = 0;
clslist.Add(tempindex);
aa = "";
}
}
if (f > 0)
{
aa += paragraph.Range.Text;
}
}
clslist.Add(aa);
List<string> ClausesNotPresent = new List<string>();
List<string> ClausesPresent = new List<string>();
for (int i = 0; i < clslist.Count; i++)
{
for (int j = 0; j < details.Count; j++)
{
if (clslist[i].Contains(details[j].clausetitle) || clslist[i].Contains(details[j].clause))
{
ClausesPresent.Add(details[j].clausetitle + "\n" + details[j].clause);
}
}
}
List<string> test = new List<string>();
for (int z = 0; z < details.Count; z++)
{
test.Add(details[z].clausetitle + "\n" + details[z].clause);
}
if (ClausesPresent.Count < details.Count)
{
ClausesNotPresent.AddRange(test.Except(ClausesPresent));
}
for(int m=0;m<ClausesNotPresent.Count;m++)
{
ClausesNotPresent[m] = "\n"+ ClausesNotPresent[m];
}
clauseList.Clear();
for (int i = 0; i < ClausesNotPresent.Count; i++)
{
clauseList.Items.Add(ClausesNotPresent[i]);
}
}

edit listview row items or adding to it

Im trying to edit a listview row's items names.
This is the function i use to add the items to list view
public static void AddRow(this ListView lvw, int image_index,
string item_title, params string[] subitem_titles)
{
// Make the item.
ListViewItem new_item = lvw.Items.Add(item_title, 1);
// Set the item's image index.
new_item.ImageIndex = image_index;
// Make the sub-items.
for (int i = subitem_titles.GetLowerBound(0);
i <= subitem_titles.GetUpperBound(0);
i++)
{
new_item.SubItems.Add(subitem_titles[i]);
}
}
This is what i have tried so far, Trying to edit the text or even add new items
public static void EditRow(this ListView lvw, int image_index,
string item_title, params string[] subitem_titles)
{
//ListViewItem lvi = lvw.Items[image_index];
for (int i = 0; i < lvw.Items[image_index].SubItems.Count; i++)
{
lvw.Items[image_index].SubItems[i].Text = subitem_titles[i];
}
}
.
Update:
Think i have worked it out
public static void EditRow(this ListView lvw, int image_index,
string item_title, params string[] subitem_titles)
{
if (lvw.InvokeRequired)
lvw.Invoke(new MethodInvoker(delegate
{
for (int i = subitem_titles.GetLowerBound(0); i <= subitem_titles.GetUpperBound(0); i++)
lvw.Items[image_index].SubItems[i + 1].Text = subitem_titles[i];
}));
else
{
for (int i = subitem_titles.GetLowerBound(0);i <= subitem_titles.GetUpperBound(0);i++)
lvw.Items[image_index].SubItems[i + 1].Text = subitem_titles[i];
}
}

Having result even by non exist inputs

I have a text file data set with following format (the separation character is tab).
0 762354
1 645645
2 4356743
3 576899063
4 64378
.....
that I read it and save it in array by:
for (int klk = 0; klk <= 92159; klk++)
{
lineuserori = fileuserori.ReadLine();
if (!string.IsNullOrEmpty(lineuserori))
{
string[] valuesiesi = lineitemori.Split('\t');
int useridori;
foreach (string value in valuesiesi)
{
useridori = Convert.ToInt32(valuesiesi[1]);
d[klk] = useridori;
}
}
}
NOW, I want to read an input and search for it in array d, if the number exist in array, I do my calculation, if it is not in array show MessageBox.Show("Error");, the problem is, it always show output with every input (even the input does not exist in array) and never show MessageBox.Show("Error");
{
int sc = Convert.ToInt32(txtbx_id.Text);
int n = Convert.ToInt32(txtbx_noofrecomm.Text);
for (int yu = 0; yu <= 92161; yu++)
{
int wer = d[yu];
if (wer == sc)
{
userseq = yu;
break;
}
}
if (userseq >= 0 && userseq <= 92161)
{
var results = new List<float>(1143600);
for (int z = 0; z < 1143600; z++)
{
results.Add(dotproduct(userseq, z));
}
var sb1 = new StringBuilder();
foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(n))
{
sb1.AppendFormat(CultureInfo.InvariantCulture, "{0}: {1}", c[resultwithindex.Index], resultwithindex.result);
sb1.AppendLine();
}
MessageBox.Show(sb1.ToString());
}
if (userseq < 0 || userseq > 92161)
{
MessageBox.Show("Error");
}
}
Any idea
Thanks
In your code to create array , is lineitemori right?
I think, it should be lineuserori.
Replace this:
string[] valuesiesi = lineitemori.Split('\t');
with:
string[] valuesiesi = lineuserori.Split('\t');
I wrote code below. (some lines uses unknown variables commented out)
Input 762354 to txtbx_id and click button1 then empty MessageBox appeared.
Input 76235 to txtbx_id and click button1 then MessageBox say 'Error'.
public partial class Form1 : Form {
// form have three controls txtbx_id, txtbx_noofrecomm and button1.
int[] d = new int[92162];
string data =
"0\t762354\n"
+"1\t645645\n"
+"2\t4356743\n"
+"3\t576899063\n"
+"4\t64378\n";
public Form1() {
InitializeComponent();
using (var fileuserori = new StringReader(data)) { // use StringReader instead of StreamReader
string lineuserori = "";
for (int klk = 0; klk <= 92159; klk++) {
lineuserori = fileuserori.ReadLine();
if (!string.IsNullOrEmpty(lineuserori)) {
// string[] valuesiesi = lineitemori.Split('\t');
string[] valuesiesi = lineuserori.Split('\t');
int useridori;
foreach (string value in valuesiesi) {
useridori = Convert.ToInt32(valuesiesi[1]);
d[klk] = useridori;
}
}
}
}
}
private void button1_Click(object sender, EventArgs e) {
var userseq = -1;
int sc = Convert.ToInt32(txtbx_id.Text);
int n = Convert.ToInt32(txtbx_noofrecomm.Text);
for (int yu = 0; yu <= 92161; yu++) {
int wer = d[yu];
if (wer == sc) {
userseq = yu;
break;
}
}
if (userseq >= 0 && userseq <= 92161) {
var results = new List<float>(1143600);
for (int z = 0; z < 1143600; z++) {
// results.Add(dotproduct(userseq, z));
}
var sb1 = new StringBuilder();
foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(n)) {
// sb1.AppendFormat(CultureInfo.InvariantCulture, "{0}: {1}", c[resultwithindex.Index], resultwithindex.result);
// sb1.AppendLine();
}
MessageBox.Show(sb1.ToString());
}
if (userseq < 0 || userseq > 92161) {
MessageBox.Show("Error");
}
}
}

RichTextBox --> Change Font for specific line

I want to change the font in a specific line. Here's my code...
string[] textBoxLines = richTextBox1.Lines;
foreach (string line in textBoxLines)
{
if(line.StartsWith("-->"))
{
//here is my problem, how to can change the font in the spectific line...
}
}
string[] textBoxLines = richTextBox1.Lines;
for (int i = 0; i < textBoxLines.Length; i++)
{
string line = textBoxLines[i];
if (line.StartsWith("-->"))
{
richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexFromLine(i);
richTextBox1.SelectionLength = line.Length;
richTextBox1.SelectionFont = yourFont;
}
}
richTextBox1.SelectionLength = 0;//Unselect the selection

Categories