List: Insert new item after the currently pointed to item - c#

I am currently working/experimenting with lists. I am able to determine with function GetNextTree the position of each item in my list such as: First, Next and Last. I have an already made list from an array ax but now I am trying to implement an insert button that will take values tree_type, tree_height, tree_price, tree_instock and create the item. Since I can point anywhere in my list, the insert will be intended to add an item after the currently pointed to item. That is where my question is: How can I add a new item after the currently pointed to item?
public class fruit_trees
{
private string tree_type = " ";
private int tree_height = 0;
public double tree_price = 0;
private int tree_instock = 0;
public fruit_trees next_tree;
public fruit_trees(string newtree, int newheight, double newprice, int newinstock)
{
tree_type = newtree;
tree_height = newheight;
tree_price = newprice;
tree_instock = newinstock;
next_tree = null;
}
public string GetTreeType
{
get { return tree_type;
}
}
public override string ToString()
{
return tree_type + " " + tree_height + " " + tree_price + " " + tree_instock;
}
}
public class ListForTrees
{
private fruit_trees first_tree;
public fruit_trees First_tree
{
get
{
return first_tree;
}
}
public fruit_trees last_tree;
public int current;
public int count = 0;
public ListForTrees(fruit_trees new_tree)
{
first_tree = new_tree;
last_tree = new_tree;
count = 1;
current = 0;
}
public ListForTrees(IEnumerable trees)
{
current = 0;
foreach (fruit_trees t in trees)
{
this.AddTree(t);
}
}
public fruit_trees GetNextTree()
{
//current = 0;
fruit_trees ft = first_tree;
if (current == count)
{
current = 0;
}
int i = 0;
while (i != current)
{
ft = ft.next_tree;
i++;
}
return ft;
}
}
ListForTrees mainlist = new ListForTrees();
private void BtnInsertTree_Click(object sender, EventArgs e)
{
try
{
int height = Convert.ToInt32(TxtTreeHeight.Text);
int stock = Convert.ToInt32(TxtTreeStock.Text);
double price = Convert.ToDouble(TxtTreePrice.Text);
fruit_trees treeadd = new fruit_trees(TxtTreeName.Text, height, price, stock);
mainlist.AddTree(treeadd);
}
catch
{
MessageBox.Show("Please check intput fields");
}
}
private void BtnGo_Click(object sender, EventArgs e)
{
fruit_trees[] ax = { new fruit_trees("cherry", 48, 12.95, 3),
new fruit_trees("pine", 36, 9.95, 8),
new fruit_trees("oak", 60, 14.95, 2),
new fruit_trees("peach", 54, 19.95, 3),
new fruit_trees("pear", 36, 11.85, 2),
new fruit_trees("apple", 62, 13.45, 5)
};
mainlist = new ListForTrees(ax);
fruit_trees current = mainlist.First_tree;
while (current != null)
{
TxtOutput.AppendText(current.ToString() + Environment.NewLine);
current = current.next_tree;
}
}
private void ShowFirstItem_Click(object sender, EventArgs e)
{
//Show Next Item
labelSpecificTree.Text = mainlist.First_tree.GetTreeType;
}
private void ShowNextItem_Click(object sender, EventArgs e)
{
fruit_trees obj = mainlist.GetNextTree();
if (obj.next_tree == null)
{
labelSpecificTree.Text = mainlist.First_tree.GetTreeType.ToString();
}
else
{
mainlist.current++;
labelSpecificTree.Text = obj.next_tree.GetTreeType.ToString();
}
}
private void ShowLastItem_Click(object sender, EventArgs e)
{
// Show First Item
labelSpecificTree.Text = mainlist.last_tree.GetTreeType;
}

So I'm slightly confused by what's going on here. You're implementing your own ArrayList like class which is not really a list at all... I don't really see the point of doing such things in C# but alright... Here's the method;
fruit_trees[] InsertNode(fruit_trees current, fruit_trees nNode)
{
fruit_trees[] temp = new fruit_trees[ax.Length + 1];
int i = 0;
while (ax[i] != current)
{
temp[i] = ax[i];
i++;
}
temp[i+1] = nNode;
while (i < ax.Length)
{
temp[i+1] = ax[i];
}
return temp;
}
call this like such;
ax = InsertNode(current, nNode);
What we're doing is making a new list with one extra slot, copying each fruit_tree over til we hit the node we want to insert after, then we copy the new tree in. After that we copy over the rest of the old array. Then we return the new array.

Related

How to bind dynamically created text box?

I'm trying to create pricelist for hotel. I'm having a list of dates, and list of room types in hotel. That lists can contain random number of elements. That is created dynamically, and here is the code:
private void CreateControls() { var colIndex = 0;
var vrsteSoba = _Presenter.VrstaSobeDto.ToArray();
foreach (var bindingItem in vrsteSoba)
{
var lbl = new Label()
{
Width = LABEL_WIDTH,
Height = LABEL_HEIGHT - 5,
Left = 10,
Top = 30 + colIndex * (EDIT_BOX_HEIGHT + SPACE_BETWEEN_CONTROL),
Text = bindingItem
};
_dataPanel.Controls.Add(lbl);
colIndex++;
}
int a = 1;
foreach (var date in _Presenter.CeneTarifa)
{
int y = 0;
var panel = new Panel
{
Height = PANEL_HEIGHT * (vrsteSoba.Length-4),
Width = EDIT_BOX_WIDTH,
Left = a * (EDIT_BOX_WIDTH + SPACE_BETWEEN_CONTROL + 50),
Top = 5
};
_dataPanel.Controls.Add(panel);
var label = new Label
{
Height = EDIT_BOX_HEIGHT,
Location = new Point(0, 10),
Text = date.Datum,
Margin = new Padding(0)
};
panel.Controls.Add(label);
int index = 0;
foreach (var item in date.VrstaSobeCena)
{
var box = new TextBox();
panel.Controls.Add(box);
box.Height = EDIT_BOX_HEIGHT;
box.Width = EDIT_BOX_WIDTH;
box.Location = new Point(0, 30 + y * (EDIT_BOX_HEIGHT + SPACE_BETWEEN_CONTROL));
box.DataBindings.Add(new Binding(nameof(box.Text), date, date.Cena[index].Cena1));
y++;
index++;
}
++a;
}
_dataPanel.AutoScroll = true;
}`
Here is image of that representation.
Now I'm facing a problem of data binding. I need to bind price, two way, for each text box. And I'm stuck.
I have tried to bind it to property name, but then all boxes get same value. If I try to bind it to value via index, I'm getting error
Cannot bind to the property or column 34 on the DataSource. Parameter name: dataMember
Code below is used to fill model that is used in presenter
` private void FillCenePoTarifi() { var sobeArr = VrstaSobeDto.ToArray();
foreach (var datum in Datumi)
{
var dictionary = new Dictionary<string, decimal>();
var cene = new List<Cena>();
foreach (var item in sobeArr)
{
var tarif = _Tarife.Where(x => x.SifTarife == item).FirstOrDefault();
if (tarif != null)
_SastavTarife = HotelierServerLocal.Default.TarifaViewBlo.GetSastaveTarife(tarif.IdTarife);
//proveriti ovu logiku
var cena = _SastavTarife.Where(x => x.Cena1 != 0).Select(c => c.Cena1).FirstOrDefault();
cene.Add(new Cena { Cena1 = cena.ToString()});
dictionary.Add(item, cena);
}
var model = new CenePoTarifi
{
Datum = datum,
VrstaSobeCena = dictionary,
Cena = cene
};
CeneTarifa.Add(model);
}
}`
Finally here are classes that use as model.
` public class CenePoTarifi{
public Dictionary<string, decimal> VrstaSobeCena { get; set; } = new Dictionary<string, decimal>();
public string Datum { get; set; }
private List<Cena> _Cena;
public List<Cena> Cena
{
get => _Cena;
set
{
_Cena = value;
NotifyPropertyChanged("Cena");
}
}
public class Cena :
{
private string _Cena1;
public string Cena1
{
get => _Cena1;
set
{
_Cena = value;
NotifyPropertyChanged("Cena1");
}
}
}`
Does anyone has any suggestions?
Your question is: How to bind dynamically created text box. Here is one tested way for accomplishing that specific task.
First create some textboxes dynamically:
public MainForm()
{
InitializeComponent();
buttonRandom.Click += (sender, e) => generateRandomList();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
List<TextBox> tmp = new List<TextBox>();
for (int column = 1; column < tableLayoutPanel.ColumnCount; column++)
{
for (int row = 1; row < tableLayoutPanel.RowCount; row++)
{
TextBox textBox = new TextBox { Anchor = (AnchorStyles)0xF };
tableLayoutPanel.Controls.Add(textBox, column, row);
tmp.Add(textBox);
textBox.KeyDown += onAnyTextBoxKeyDown;
}
}
_textboxes = tmp.ToArray();
// Generate first dataset
generateRandomList();
}
TextBox[] _textboxes = null;
Then, whenever a new random list is generated, clear any old text and databindings from every TextBox before creating a new data binding for it.
public static Random Rando { get; } = new Random(2);
private void generateRandomList()
{
// Clear ALL the data + bindings for ALL the textboxes.
foreach (var textbox in _textboxes)
{
textbox.Clear();
textbox.DataBindings.Clear();
}
// Generate and create new bindings
int count = Rando.Next(1, 79);
for (int i = 0; i < count; i++)
{
var textbox = _textboxes[i];
VrstaSobeCena vrstaSobeCena =
new VrstaSobeCena{ Sobe = (Sobe)tableLayoutPanel.GetRow(textbox) };
textbox.Tag = vrstaSobeCena;
textbox.DataBindings.Add(
new Binding(
nameof(TextBox.Text),
vrstaSobeCena,
nameof(VrstaSobeCena.Cena),
formattingEnabled: true,
dataSourceUpdateMode: DataSourceUpdateMode.OnPropertyChanged,
null,
"F2"
));
// TO DO
// ADD vrstaSobeCena HERE to the Dictionary<string, decimal> VrstaSobeCena
}
}
The classes shown in your code as binding sources may not bind correctly. One issue I noticed is that the property setters are failing to check whether the value has actually changed before firing the notification. Here's an example of doing that correctly. (For testing purposes I'm showing a Minimal Reproducible Sample "mock" version of a class that implements INotifyPropertyChanged.)
enum Sobe { APP4 = 1, APP5, STUDIO, SUP, APP6, STAND, STDNT, COMSTU, LUXSTU, APP4C, APP4L, APP62, APP6L }
class VrstaSobeCena : INotifyPropertyChanged
{
decimal _price = 100 + (50 * (decimal)Rando.NextDouble());
public decimal Cena
{
get => _price;
set
{
if (!Equals(_price, value))
{
_price = value;
OnPropertyChanged();
}
}
}
Sobe _sobe = 0;
public Sobe Sobe
{
get => _sobe;
set
{
if (!Equals(_sobe, value))
{
_sobe = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Finally, one way to test the two-way binding is to intercept the [Enter] key.
private void onAnyTextBoxKeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.Enter) && (sender is TextBox textbox))
{
e.SuppressKeyPress = e.Handled = true;
VrstaSobeCena vrstaSobeCena = (VrstaSobeCena)textbox.Tag;
string msg = $"Price for {vrstaSobeCena.Sobe} is {vrstaSobeCena.Cena.ToString("F2")}";
BeginInvoke((MethodInvoker)delegate {MessageBox.Show(msg); });
SelectNextControl(textbox, forward: true, tabStopOnly: true, nested: false, wrap: true);
}
}
Create a List for storing the textbox:
List<TextBox> lstTextbox = new List<TextBox>();
Create a class object that stores the values of "Date" and "room type"
public class RoomTypeDate
{
public string RoomType = "";
public string DateRange = "";
}
Immediately after you created the textbox, assigned the RoomTypeDate info to the tag, add it to lstTextbox.
foreach (var item in date.VrstaSobeCena)
{
var box = new TextBox();
panel.Controls.Add(box);
box.Height = EDIT_BOX_HEIGHT;
box.Width = EDIT_BOX_WIDTH;
box.Location = new Point(0, 30 + y * (EDIT_BOX_HEIGHT + SPACE_BETWEEN_CONTROL));
box.DataBindings.Add(new Binding(nameof(box.Text), date, date.Cena[index].Cena1));
// add the box to the list
lstTextbox.Add(box);
// mark the box with RoomType and DateRange
RoomTypeDate rtd = new RoomTypeDate();
rtd.RoomType = "APP4"; // get the room type
rtd.DateRange = "1.6 - 30.6"; // get date range
box.Tag = rtd;
y++;
index++;
}
Now, to get and set the room price:
public void SetRoomPrice(decimal price, string roomType, string dateRange)
{
foreach (var tb in lstTextBox)
{
var rtd = (RoomTypeDate)tb.Tag;
if(rtd.RoomType == roomType && rtd.DateRange == dateRange)
{
tb.Text = price.ToString();
return;
}
}
}
public decimal GetRoomPrice(string roomType, string dateRange)
{
foreach (var tb in lstTextBox)
{
var rtd = (RoomTypeDate)tb.Tag;
if(rtd.RoomType == roomType && rtd.DateRange == dateRange)
{
return Convert.ToDecimal(rt.Text);
}
}
return 0m;
}
*code untested, might contains bugs

C# search checklistbox on textbox but don't remember checked or copy record twice to checklistbox C#

i have problems with searching checklistbox in textbox. When I have 1 parameter
ladujZBazy(string mustContains)
When I checked items on list in checkedListBox1 and search some items using textBox1 my previous check is gone.
I add 2nd parameter to your function(bool type)
void ladujZBazy(string mustContains, bool dropIndexes)
which will by default be false
private void dbopakowania_Load(object sender, EventArgs e)
{
ladujZBazy(null, false);
}
and And then call textbox's TextChanged event as false.
private void textBox1_TextChanged(object sender, EventArgs e)
{
ladujZBazy(textBox1.Text, false); //false
}
This time checked is not gone but copy record twice to checklistbox.. When value boolean is true I checked items on list in checkedListBox1 and search some items using textBox1 my previous check is gone. I want when i checked and search some items using textBox1 my previous checked is not gone and don't copy record twice to checklistbox.
Full Code:
namespace Email_Sender
{
public partial class dbopakowania : Form
{
EmailSender emailsender;
public List<List<string>> listOpakowaniaTabela = new List<List<string>>();
public string doZamowienia = "";
List<int> indexes = new List<int>();
string typZgloszenia;
public dbopakowania(EmailSender _emailsender, string _typZgloszenia)
{
InitializeComponent();
this.emailsender = _emailsender; //przechwycenie obiektu EmailSender do lokalnego obiektu tego samego typu
this.typZgloszenia = _typZgloszenia;
if (typZgloszenia == "ZWROT")
{
label1.Text = "Zwróć opakowania:";
btnGetItem.Text = "Zwróć";
this.emailsender.txt_subject.Text = "Zwrot opakowań";
}
else if(typZgloszenia == "ZAMOWIENIE")
{
label1.Text = "Zamów opakowania:";
btnGetItem.Text = "Zamów";
this.emailsender.txt_subject.Text = "Zamówienie opakowań";
}
}
private void dbopakowania_Load(object sender, EventArgs e)
{
ladujZBazy(null, false);
}
private void button2_Click(object sender, EventArgs e)
{
if (label1.Text == "Zamów opakowania:")
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
{
AddValueToZamówienieOpakowan oknoZamowieniaOpakowania_Szczegoly = new AddValueToZamówienieOpakowan(this, indexes[i]);
oknoZamowieniaOpakowania_Szczegoly.ShowDialog();
if (doZamowienia != "")
{
this.emailsender.pozycja++;
this.emailsender.txt_msg.Text += emailsender.pozycja.ToString() + "." + " " + doZamowienia;
//this.emailsender.txt_subject.Clear();
//this.emailsender.txt_subject.Text = "Zamówienie opakowań";
}
}
}
this.Close();
}
else if (label1.Text == "Zwróć opakowania:")
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
{
ZwrotOpakowan oknoZamowieniaOpakowania_Szczegoly = new ZwrotOpakowan(this, indexes[i]);
oknoZamowieniaOpakowania_Szczegoly.ShowDialog();
if (doZamowienia != "")
{
this.emailsender.pozycja++;
this.emailsender.txt_msg.Text += emailsender.pozycja.ToString() + "." + " " + doZamowienia;
//this.emailsender.txt_subject.Clear();
//this.emailsender.txt_subject.Text = "Zamówienie opakowań";
}
}
}
this.Close();
}
else
{
MessageBox.Show("Nieoczekiwany Błąd - skontaktuj sie z admin", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btn_close_Click(object sender, EventArgs e)
{
this.Close();
}
void ladujZBazy(string mustContains, bool dropIndexes)
{
if (dropIndexes)
{
checkedListBox1.Items.Clear();
listOpakowaniaTabela.Clear();
indexes.Clear();
}
bazaproduktowDBEntities dc = new bazaproduktowDBEntities();
var c1 = from d in dc.OpakowaniaTabela select d.NazwaOpakowania;
var c2 = from d in dc.OpakowaniaTabela select "(" + d.PartiaOpakowania + ")";
var c3 = from d in dc.OpakowaniaTabela select d.IloscOpakowania;
var c4 = from d in dc.OpakowaniaTabela select d.JednostkaOpakowania;
listOpakowaniaTabela.Add(c1.ToList());
listOpakowaniaTabela.Add(c2.ToList());
listOpakowaniaTabela.Add(c3.ToList());
listOpakowaniaTabela.Add(c4.ToList());
for (int i = 0; i < listOpakowaniaTabela[0].Count; i++)
{
string strToAdd = "";
for (int j = 0; j < listOpakowaniaTabela.Count; j++)
{
strToAdd += " " + listOpakowaniaTabela[j][i] + " ";
}
if (mustContains == null)
{
checkedListBox1.Items.Add(strToAdd);
indexes.Add(i);
}
else if (strToAdd.ToLower().Contains(mustContains.ToLower()))
{
checkedListBox1.Items.Add(strToAdd);
indexes.Add(i);
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
ladujZBazy(textBox1.Text, false); //false
}
}
Any Solution? C#

Error messege "The source contains no DataRows" when i sent a data table object to a pagenation Class method

I have a User Control with a DataGrid.Every thing was fine since the DataGride Pagenation Function within the User Control Class.I Desided to move the pagenation functions into a class instead.This is my Code
{
private static DataTable _dt ;
private static TextBlock _PageInformation;
private static ComboBox _NumberOfRecords;
private static Button _btnPrev;
private static Button _BtnFirst;
private static Button _BtnNext;
private static Button _BtnLast;
internal static AssetsManagement equipmentManage
{
get { return equipmentMng; }
set { equipmentMng = value; }
}
private static DataGrid _datagridObject;
public static TextBlock PageInformation { get => _PageInformation; set => _PageInformation = value; }
public static DataTable dt { get => _dt; set => _dt = value; }
public static ComboBox NumberOfRecords { get => _NumberOfRecords; set => _NumberOfRecords = value; }
public static DataGrid DatagridObject { get => _datagridObject; set => _datagridObject = value; }
public static Button BtnPrev { get => _btnPrev; set => _btnPrev = value; }
public static Button BtnFirst { get => _BtnFirst; set => _BtnFirst = value; }
public static Button BtnNext { get => _BtnNext; set => _BtnNext = value; }
public static Button BtnLast { get => _BtnLast; set => _BtnLast = value; }
int pageIndex = 1;
public static int numberOfRecPerPage;
//To check the paging direction according to use selection.
private enum PagingMode
{ First = 1, Next = 2, Previous = 3, Last = 4, PageCountChange = 5 };
public Pagenation(DataGrid UCDatagrid,ComboBox NumberOfRecordsComboBox,TextBlock PageInfo,DataTable Data,Button Prev, Button First, Button Next, Button Last)
{
DatagridObject = UCDatagrid;
dt = Data.Copy();
if (Data == null)
{
MessageBox.Show(Data.Rows.Count.ToString());
}
PageInformation = PageInfo;
NumberOfRecords = NumberOfRecordsComboBox;
Prev = BtnPrev;
First = BtnFirst;
Next= BtnNext;
Last= BtnLast;
NumberOfRecordsComboBox.Items.Add("5");
NumberOfRecordsComboBox.Items.Add("10");
NumberOfRecordsComboBox.Items.Add("15");
NumberOfRecordsComboBox.Items.Add("20");
NumberOfRecordsComboBox.Items.Add("25");
RefreshDataGrid(dt);
}
public void BtnFirst_Click(object sender, RoutedEventArgs e)
{
Navigate((int)PagingMode.First);
}
public void BtnNext_Click()
{
Navigate((int)PagingMode.Next);
}
public void BtnPrev_Click(object sender, RoutedEventArgs e)
{
Navigate((int)PagingMode.Previous);
}
public void BtnLast_Click(object sender, RoutedEventArgs e)
{
Navigate((int)PagingMode.Last);
}
public void CbNumberOfRecords_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Navigate((int)PagingMode.PageCountChange);
}
private void Navigate(int mode)
{
int count;
switch (mode)
{
case (int)PagingMode.Next:
BtnPrev.IsEnabled = true;
BtnFirst.IsEnabled = true;
if (dt.Rows.Count >= (pageIndex * numberOfRecPerPage))
{
if (dt.AsEnumerable().Skip(pageIndex *
numberOfRecPerPage).Take(numberOfRecPerPage).Count() == 0)
{
DatagridObject.ItemsSource = null;
DatagridObject.ItemsSource = dt.Rows.Count <= 0 ? null : dt.AsEnumerable().Skip((pageIndex *
numberOfRecPerPage) - numberOfRecPerPage).Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
count = (pageIndex * numberOfRecPerPage) +
(dt.AsEnumerable().Skip(pageIndex *
numberOfRecPerPage).Take(numberOfRecPerPage)).Count();
}
else
{
DatagridObject.ItemsSource = null;
DatagridObject.ItemsSource = dt.Rows.Count <= 0 ? null : dt.AsEnumerable().Skip(pageIndex *
numberOfRecPerPage).Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
count = (pageIndex * numberOfRecPerPage) +
(dt.AsEnumerable().Skip(pageIndex * numberOfRecPerPage).Take(numberOfRecPerPage)).Count();
pageIndex++;
}
PageInformation.Text = count + " of " + dt.Rows.Count;
}
else
{
BtnNext.IsEnabled = false;
BtnLast.IsEnabled = false;
}
break;
case (int)PagingMode.Previous:
BtnNext.IsEnabled = true;
BtnLast.IsEnabled = true;
if (pageIndex > 1)
{
pageIndex -= 1;
DatagridObject.ItemsSource = null;
if (pageIndex == 1)
{
DatagridObject.ItemsSource = dt.Rows.Count <= 0 ? null : dt.AsEnumerable().Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
count = dt.AsEnumerable().Take(numberOfRecPerPage).Count();
PageInformation.Text = count + " of " + dt.Rows.Count;
}
else
{
DatagridObject.ItemsSource = dt.Rows.Count <= 0 ? null : dt.AsEnumerable().Skip
(pageIndex * numberOfRecPerPage).Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
count = Math.Min(pageIndex * numberOfRecPerPage, dt.Rows.Count);
PageInformation.Text = count + " of " + dt.Rows.Count;
}
}
else
{
BtnPrev.IsEnabled = false;
BtnFirst.IsEnabled = false;
}
break;
case (int)PagingMode.First:
pageIndex = 2;
Navigate((int)PagingMode.Previous);
break;
case (int)PagingMode.Last:
pageIndex = (dt.Rows.Count / numberOfRecPerPage);
Navigate((int)PagingMode.Next);
break;
case (int)PagingMode.PageCountChange:
numberOfRecPerPage = Convert.ToInt32(NumberOfRecords.SelectedItem);
DatagridObject.ItemsSource = null;
DatagridObject.ItemsSource = dt.Rows.Count <= 0 ? null : dt.AsEnumerable().Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
count = (dt.AsEnumerable().Take(numberOfRecPerPage)).Count();
PageInformation.Text = count + " of " + dt.Rows.Count;
BtnNext.IsEnabled = true;
BtnLast.IsEnabled = true;
BtnPrev.IsEnabled = true;
BtnFirst.IsEnabled = true;
break;
}
}
public static void RefreshDataGrid(DataTable dte)
{
MessageBox.Show(dte.Rows.Count.ToString());
dt = dte.Copy();
MessageBox.Show(dt.Rows.Count.ToString());
DatagridObject.ItemsSource = null;
DatagridObject.ItemsSource = dt.AsEnumerable().Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
int count = dt.AsEnumerable().Take(numberOfRecPerPage).Count();
PageInformation.Text = count + " of " + dt.Rows.Count;
}
}}
and in the Class of the UserControl I instantaite an object of the Pagenation Class with the constructor that takes all the needed varaible for pagenation.
public PartsUC()
{
InitializeComponent();
PartsDataTable = PartMng.GetAllEquipments();
MessageBox.Show(PartsDataTable.Rows.Count.ToString());
PageInformation = lblpageInformation;
Partsdatagrid = this.PartsDataGrid;
CbNumberOfRecords = this.cbNumberOfRecords;//the interface XAMAL Combobox
Pagenation pagenation = new Pagenation(Partsdatagrid, cbNumberOfRecords, PageInformation, PartsDataTable, btnPrev, btnFirst, btnNext, btnLast);
//Pagenation.RefreshDataGrid(PartsDataTable);}
The problem is that i still getting this error message!
The source contains no DataRows
and the error points to the refreshDataGrid method in this line
DatagridObject.ItemsSource = dt.AsEnumerable().Take(numberOfRecPerPage).CopyToDataTable().DefaultView;
Note that the messege box in the function refreshDataGrid shows that i have 50 rows.
Solved it,The thing that caused the error was the function Take() since it takes the number from the combobox that let you choose how many rows to display.I put Fixed number and It Worked.
DatagridObject.ItemsSource = dt.AsEnumerable().Take(15).CopyToDataTable().DefaultView;

Loop is not storing data in array correctly C#

Hey guys I'm currently working on a bank program for a class project. The idea the user will need to make an account if not done so already but if they already do they can just login using account number and pin. However. instead of my program constantly adding data to an array that's size is 100 it just replaces the data in slot [0] just wondering why.
public partial class Form1 : MetroForm
{
//For Creating new account
string newAccountType;
Accounts[] customers = new Accounts[99999];
int temp;
string VerifyPin = ("");
private void openAccount_Click(object sender, EventArgs e)
{
for (int index=0;index < customers.Length; ++index)
{
var R1 = new Random();
var R2 = new Random();
customers[index] = new Accounts();
customers[index].Name = newName.Text;
customers[index].accountType = newAccountType;
customers[index].accountNumber = (R1.Next(1000000,9000000))+(R2.Next(100,9000));
customers[index].accountPin = createPin.Text;
customers[index].accountBalance = 100.00;
temp = index;
}
MetroMessageBox.Show(this, "Thank you member "+customers[temp].Name+"\nYour member number is: "+customers[temp].accountNumber, "You are now a memeber", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
metroTabControl1.SelectedTab = metroTabPage2;
}
private void checkBalance_Click(object sender, EventArgs e)
{
int veri=0;
bool isfound = false;
for (int count = 0; count < customers.Length; ++count)
{
if (Convert.ToInt32(userName.Text) == customers[count].accountNumber)
{
veri = count;
isfound = true;
}
else
isfound = false;
accountnotfound.Text = "Account Not Found";
}
if (isfound && (customers[veri].accountPin == pinText.Text))
{
MetroMessageBox.Show(this, "account found", "account found");
}
else
{
MetroMessageBox.Show(this, "account not found or wrong pin", "account not found");
pinText.Text = "";
}
accountBalance.Visible = true;
userWithdraw.Visible = true;
userDeposite.Visible = true;
accountBalance.Text = "Welcome, "+customers[veri].Name+"\nYour current balance is: "+customers[veri].accountBalance;
}
public class Accounts
{
private string name, AccountType, AccountPin;
private int AccountNumber;
private double AccountBalance;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int accountNumber
{
get
{
return AccountNumber;
}
set
{
AccountNumber = value;
}
}
public string accountPin
{
get
{
return AccountPin;
}
set
{
AccountPin = value;
}
}
public string accountType
{
get
{
return AccountType;
}
set
{
AccountType = value;
}
}
public double accountBalance
{
get
{
return AccountBalance;
}
set
{
AccountBalance = value;
}
}
}
I think you just need to keep track of which element in the array is available (i.e. has a null value). Once no elements in the array are null, this means that the customers array is full, so you can't add any more customers at that point.
Make these following changes. In the part of the code just where the code starts the loop:
bool added = false;
for (int index=0;index < customers.Length; ++index)
{
if (customers[index] != null) continue;
...
Also, after you actually add a customer, do this (right after the loop ends):
if (!added)
{
// show error message here!
}
else
{
MetroMessageBox.Show(this, "Thank you member "+customers[temp].Name+"\nYour member number is: "+customers[temp].accountNumber, "You are now a memeber", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
metroTabControl1.SelectedTab = metroTabPage2;
}
for (int index=0;index < customers.Length; ++index)
customers[index] = new Accounts();
Index will be 0 every time your array loops, thus replacing customers[0] every time you openAccount_Click is called.
You don't really need to loop to add. You just need to make sure you are writing the next possible index. Maybe use your Temp variable and increment it + 1 for every user added and then without a loop do like do.
customers[Temp] = new Accounts();
customers[Temp].Name = newName.Text;
customers[Temp].accountType = newAccountType;
customers[Temp].accountNumber = (R1.Next(1000000,9000000))+ (R2.Next(100,9000));
customers[Temp].accountPin = createPin.Text;
customers[Temp].accountBalance = 100.00;
Temp += 1;
You would be better off using a List of Accounts for your customers global instead.
List<Accounts> customers = new List<Accounts>();
Then inside openAccount_Click you can add a new Accounts like so.
customers.Add(new Accounts {
Name = newName.Text,
accountType = newAccountType,
accountNumber = (R1.Next(1000000,9000000))+(R2.Next(100,9000)),
accountPin = createPin.Text,
accountBalance = 100.00
});
bool added = false;
for (int x = 0; x <= temp; x++)
{
if (customers[temp] != null) continue;
{
for (int indexies = 0; indexies < customers.Length; indexies++)
{
var R1 = new Random();
var R2 = new Random();
Convert.ToInt32(createPin.Text);
customers[temp] = new Accounts();
customers[temp].Name = newName.Text;
customers[temp].accountType = newAccountType;
customers[temp].accountNumber = 1;//(R1.Next(1000000, 9000000)) + (R2.Next(100, 9000));
customers[temp].accountPin = Convert.ToInt32(createPin.Text);
customers[temp].accountBalance = 100.00;
added = true;
}
}
}

Datagridview sorting column based on matching string

I have a datagridview that I would like to have rows sorted based on the portion of a string entered from a user. The entered string is compared with all of the strings in a particular column. For instance, if I gave "comp" as the search word, the program would try to compare the search word with the strings on first column and sort the rows in a descending order which starts with "comp", such as "compare", "composition", "computer" etc. Rest of the words that do not match is either left alone or sorted in an alphabetical order (whichever is easier).
In LINQ, I am aware that you can apply the following code to achieve what you wanted with a string array:
var sortedWords = words.Where(x => x.Contains("comp"))
.OrderByDescending(x => x);
How can I achieve the same thing in Datagridview as I need to have the rows sorted, not just the items inside a particular column?
Edit:
The following code is giving a System.InvalidOperationException. (SetCurrentCellAddressCore is being called twice)
private void DGVPointCtrl_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
MatchComparer mc = new MatchComparer();
DGVPointCtrl.Sort(mc); //Error
}
I'm probably doing something wrong but I'm not sure why. Here is the code that programatically adds the rows for testing purposes:
private void BtnRefresh_Click(object sender, EventArgs e)
{
try
{
DGVPointCtrl.Rows.Clear();
int mainIndex = CmbMainDevice.SelectedIndex;
int subIndex = CmbSubDevice.SelectedIndex;
DDCDAO ddcdao = new DDCDAO(DDCGlobal.ddcEngineIP, ddc.Ip);
string pointListType;
object rs;
//Currently only supports IO DDC Request
//TO DO: Change DDCDAO to send proper subdevice requests
if (mainIndex == 0) //IO
{
#region Main Device: IO
}
//First row is for searching items
DGVPointCtrl.Rows.Add(new DataGridViewRow());
for (int i = 1; i < 5; i++)
{
DGVPointCtrl.Rows.Add(new DataGridViewRow());
DGVPointCtrl.Rows[i].ReadOnly = true;
}
DGVPointCtrl.Columns[0].SortMode = DataGridViewColumnSortMode.Programmatic;
DGVPointCtrl.Rows[0].DefaultCellStyle.Font =
new Font(DGVPointCtrl.DefaultCellStyle.Font, FontStyle.Italic | FontStyle.Bold);
if (subIndex == 1) //BI
{
PointDGVColumnGenerate("IO_BI");
}
else if (subIndex == 2) //BO
{
PointDGVColumnGenerate("IO_BO");
}
else if (subIndex == 3) //AI
{
PointDGVColumnGenerate("IO_AI");
}
else if (subIndex == 4) //AO
{
PointDGVColumnGenerate("IO_AO");
}
DGVPointCtrl.Rows[1].Cells[0].Value = "IO12314";
DGVPointCtrl.Rows[2].Cells[0].Value = "IO21948";
DGVPointCtrl.Rows[3].Cells[0].Value = "IO28194";
DGVPointCtrl.Rows[4].Cells[0].Value = "VP12984";
DGVPointCtrl.Rows[2].Cells[1].Value = "asdf";
#endregion
}
catch
{
}
}
private void PointDGVColumnGenerate(string key)
{
int colCount = 0;
DGVColumnTable.Clear();
for (int i = 0; i < COL_MAX; i++)
{
DGVPointCtrl.Columns[i].HeaderText = " ";
DGVPointCtrl.Columns[i].Visible = true;
}
foreach (string s in UIConstant.DDCPCtrlListColumnText[key])
{
DGVPointCtrl.Columns[colCount].HeaderText = s;
DGVColumnTable.Add(DGVPointCtrl.Columns[colCount]);
colCount++;
}
}
Edit2:
public class MatchComparer : IComparer
{
private static IComparer defaultComparer = new CaseInsensitiveComparer();
int IComparer.Compare(object x, object y)
{
DataGridViewRow xr = (DataGridViewRow)x;
DataGridViewRow yr = (DataGridViewRow)y;
string xs = "";
string ys = "";
try
{
xs = xr.Cells[0].Value.ToString();
}
catch
{
}
try
{
ys = yr.Cells[0].Value.ToString();
}
catch
{
}
if (HasMatch(xs) && !HasMatch(ys)) return -1;
else if (!HasMatch(xs) && HasMatch(ys)) return 1;
else return defaultComparer.Compare(xs, ys);
}
This is possible only if you are populating the grid yourself as opposed to binding it to the database.
Set DataGridViewColumn.SortMode to Programmatic.
Use DataGridView.Sort to impose a comparer like this:
public class MatchComparer : IComparer {
int IComparer.Compare(object x, object y) {
if (HasMatch(x) && !HasMatch(y)) return -1;
else if (!HasMatch(x) && HasMatch(y)) return 1;
else return defaultComparer.Compare(x, y);
}
private bool HasMatch(object x) {
return x is string && ((string)x).StartsWith("comp");
}
private static IComparer defaultComparer = new CaseInsensitiveComparer();
}

Categories