I have this Code of Showing all Records to a Message box using string builder. Now i need to populate the Serialnumbers to a data grid view or list view.
Similar in My First Question
I replace message box with this code in adding the records to list:
private readonly StringBuilder _builder = new StringBuilder().AppendLine();
Private Dataset _dsdata;
private void SerialVerification()
{
// instantiate BAL
var obj = new BusinessLogic();
var fin = new Entity { SerialNumber = txtSerialNumber.Text};
try
{
try
{
//Check if Serial is on DB
_dsdata = obj.SelectSerial(fin);
if (_dsdata.Tables[0].Rows.Count < 1)
{
//Not on DB table then SAVE
try
{
//Message SAVE ON DB
_i++;
_builder.Append(txtSerialNumber.text).AppendLine();
lblcount.Text = @"Unit Count:" + _i;
}
catch (Exception ee)
{
}
}
else
{
_dates Convert.ToDateTime(_dsdata.Tables[0].Rows[0][2].ToString()).ToShortDateString();
if(_dates == DateTime.Today.ToShortDateString())
{
//Check Duplicate
}
else
{
//Message SAVE ON DB
_i++;
_builder.Append(txtSerialNumber.Text).AppendLine();
lblcount.Text = @"Unit Count:" + _i;
}
}
}
catch(Exception ee)
{
开发者_StackOverflow社区 }
if(_i>0)
{
listView1.Items.Clear();
foreach(var a in r)
{
var listItem = new ListViewItem(_builder.ToString()) ;
listView1.Items.Add(listItem);
}
//MessageBox.Show(_builder.ToString());
}
else
{
}
}
catch (ApplicationException ex)
{
}
}
It does not creating new line/Row in list view but just populating only one row. Is there any solution other than using string builder.
Entry of serialnumber: 1 2 3
My list Displays using my sample code:
1
12
123
Correct:
1
2
3
Thanks in regards..
When you write :
var r = new List<StringBuilder> { _builder };
You will always create a list with exactly 1 stringbuilder (which is _builder); no matter what the _builder instance contains. So when you iterate over this list, it will always create 1 listitem, with the full contents of your builder. So you either have to put more StringBuilders in your list, or split the contents of your _builder instance into mulitple strings and iterate over those.
Update:
After looking at your updated question:
Yes, your prev code behaves like it does because, for each call to SerialVerification method, _builder appends the items
ex: for input 1 - _builder = 1\r\n; and the listview has 1 NewLine; now for 2 - _builder = 1\r\n2\r\n and the listView has 1 NewLine 2 NewLine; same for 3.
That is the reason you see 1 , 12, 123. Why are you caching _builder or _items (in your answer post). Because you maintain a cache, every time you add a new item you are forced to clear the listview; even you have to maintain cache don't populate the listview with items from the cache;
Here is another version of SerialVerification which doesn't use _items / _builder, hope it might help.
private void SerialVerification(String serialNumber)
{
// verify that we have the serial number with us
// HasSerialNumber does a DB look up for this serial number
// and returns true / false accordingly;
Boolean hasSerialNumber = HasSerialNumber(serialNumber);
if (hasSerialNumber)
{
// verify that we don't have the serial number already in the ListView;
// if not add it to the listview;
listView1.Items.Add(serialNumber);
}
}
Is _builder a StringBuilder[] or List<StringBuilder>
's?
Are you storing one long string in each StringBuilder?
The output you want, which is showing each item in a separate line could be achieved by setting the
this.listView1.View = View.List;
But I would do something like this below to store multiple items in a StringBuilder and add it to a listview.
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("{0}|", "One");
stringBuilder.AppendFormat("{0}|", "Two");
stringBuilder.AppendFormat("{0}|", "Three");
stringBuilder.AppendFormat("{0}|", "Four");
List<String> items =
new List<String>(stringBuilder.ToString().Split(new Char[] { '|' }, StringSplitOptions.RemoveEmptyEntries));
foreach (String item in items)
{
listView1.Items.Add(item);
}
I got the answer i replace my code to this:
//private readonly StringBuilder _builder = new StringBuilder().AppendLine();
private readonly List<String> _items = new List<string>();
// _builder.Append(fin.SerialNumber).AppendLine();
_items.Add(txtSerialNumber.Text);
listView1.Items.Clear();
foreach(var item in _items)
{
listView1.Items.Add(item.ToString());
}
精彩评论