The current page I am working on is a simple page, but I cannot get it to do what I want. When the page loads the grid is blank not even showing one blank row. On the left is a list box and when the user selects a user from this box then the grid on the right side gets populated with a description.
What I want to do is, when the page loads I want the user to be able to click a button and have a row added to the grid so they can start typing descriptions or comments.
Like I said I am very new to all of this, and I have nowhere to go but the web.
below is the mainpage.xaml.cs code I have to work with.
namespace
SLScreen
{
public partial class MainPage : UserControl
{
public bool ChangeFlag;
GridPosition _NewBillBodyPOS;
public ObservableCollection<NoteSVC.TshipNoteBody> NoteBodyLines;
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
this.Cursor = Cursors.Wait;
ChangeFlag =
false;
//Set Proxy
NoteSVC.
SimpleWCFClient proxy = new SLScreen.NoteSVC.SimpleWCFClient();
//Set Completed action
proxy.GetNoteListCompleted +=
new EventHandler<SLScreen.NoteSVC.GetNoteListCompletedEventArgs>(proxy_GetNoteListCompleted);
//Make call to service
proxy.GetNoteListAsync();
proxy.FillNoteClassListCompleted +=
new EventHandler<SLScreen.NoteSVC.FillNoteClassListCompletedEventArgs>(proxy_FillNoteClassListCompleted);
proxy.FillNoteClassListAsync();
}
//WCF Call Completion Functions
void proxy_FillNoteClassListCompleted(object sender, SLScreen.NoteSVC.FillNoteClassListCompletedEventArgs e)
{
ObservableCollection<string> NoteClassList = e.Result;
this.tddNoteClass.ItemsSource = NoteClassList;
}
void proxy_GetNoteListCompleted(object sender, SLScreen.NoteSVC.GetNoteListCompletedEventArgs e)
{
ObservableCollection<NoteSVC.TShipNoteType> NoteList = e.Result;
//NotesList.Items.Clear();
lstNotes.ItemsSource = NoteList;
this.Cursor = Cursors.Arrow;
}
void proxy_DeleteNoteHdrCompleted(object sender, SLScreen.NoteSVC.DeleteNoteHdrCompletedEventArgs e)
{
string NewUpdate = e.Result.ToString();
if (NewUpdate.Substring(0, 5) == "Error")
{
if (NewUpdate == "Error: LastUpdate")
{
MessageBox.Show("Last Update Error. Please refresh your screen and re-enter data.");
}
else
{
MessageBox.Show(NewUpdate);
}
}
else
{
ResetControls();
//Set Proxy
NoteSVC.
SimpleWCFClient proxy = new SLScreen.NoteSVC.SimpleWCFClient();
//Set Completed action
proxy.GetNoteListCompleted +=
new EventHandler<SLScreen.NoteSVC.GetNoteListCompletedEventArgs>(proxy_GetNoteListCompleted);
//Make call to service
proxy.GetNoteListAsync();
}
}
void proxy_UpdateNoteHdrCompleted(object sender, SLScreen.NoteSVC.UpdateNoteHdrCompletedEventArgs e)
{
string results = e.Result.ToString();
if (results.StartsWith("Error") == true)
{
if (results == "Error: LastUpdate")
{
string msgtext = "There was an error Updating the record. another user has accessed the record. ";
msgtext = msgtext +
"Please refresh the screen and re-enter update.";
MessageBox.Show(msgtext);
}
else
{
MessageBox.Show(results);
}
}
else
{
ChangeFlag =
false;
MessageBox.Show("Changes Saved");
////reset screen
ResetControls();
//Set Proxy
NoteSVC.
SimpleWCFClient proxy = new SLScreen.NoteSVC.SimpleWCFClient();
//Set Completed action
proxy.GetNoteListCompleted +=
new EventHandler<SLScreen.NoteSVC.GetNoteListCompletedEventArgs>(proxy_GetNoteListCompleted);
//Make call to service
proxy.GetNoteListAsync();
}
}
//Control functions
private void lstNotes_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (ChangeFlag == false)
{
foreach (NoteSVC.TShipNoteType o in e.AddedItems)
{
PopulateControls(o);
}
}
else
{
string MsgText = "The data has been changed. Click OK save the changes ";
MsgText = MsgText +
"or Cancel to cancel the changes and load the selected Note.";
string CapText = "Changed DataContext Not Saved";
MessageBoxResult result = MessageBox.Show(MsgText, CapText, MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
//Save changes
ChangeFlag =
false;
ResetControls();
MessageBox.Show("Changes Saved");
}
else
{
//Delete changes and load new note
foreach (NoteSVC.TShipNoteType o in e.AddedItems)
{
PopulateControls(o);
ChangeFlag =
false;
}
ResetControls();
}
}
}
private void tddNoteClass_DropDownClosed(object sender, EventArgs e)
{
ChangeFlag =
true;
}
private void tddNoteSec_DropDownClosed(object sender, EventArgs e)
{
ChangeFlag =
true;
}
private void btnAddNote_Click(object sender, RoutedEventArgs e)
{
if (txtInvNum.Text.Length > 0)
{
}
else
{
this.grdNoteBody.BeginInsert();
}
}
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
int NoteNum = int.Parse(txtInvNum.Text);
string LastUpdateString = txtLastUpdate.Text;
NoteSVC.
SimpleWCFClient proxy = new SLScreen.NoteSVC.SimpleWCFClient();
proxy.DeleteNoteHdrCompleted +=
new EventHandler<SLScreen.NoteSVC.DeleteNoteHdrCompletedEventArgs>(proxy_DeleteNoteHdrCompleted);
proxy.DeleteNoteHdrAsync(NoteNum, LastUpdateString);
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{
ResetControls();
}
private void btnSaveEdit_Click(object sender, RoutedEventArgs e)
{
//Build object
NoteSVC.
TShipNoteType NoteToSave = new SLScreen.NoteSVC.TShipNoteType();
NoteToSave.InvNum =
int.Parse(txtInvNum.Text);
NoteToSave.NoteTitle = txtTitle.Text;
NoteToSave.NoteClass = tddNoteClass.Text;
NoteToSave.NoteLevel = tddNoteSec.SelectedIndex;
NoteToSave.LastUpdate = txtLastUpdate.Text;
NoteToSave.LastUser = txtLastUser.Text;
NoteToSave.NoteBodyLines = NoteBodyLines;
//Call Save routine and pass in NoteToSave object
NoteSVC.
SimpleWCFClient proxy = new SLScreen.NoteSVC.SimpleWCFClient();
proxy.UpdateNoteHdrCompleted +=
new EventHandler<SLScreen.NoteSVC.UpdateNoteHdrCompletedEventArgs>(proxy_UpdateNoteHdrCompleted);
proxy.UpdateNoteHdrAsync(NoteToSave);
}
private void txtTitle_KeyUp(object sender, KeyEventArgs e)
{
ButtonEnableCheck();
ChangeFlag =
true;
}
private void txtTitle_TextChanged(object sender, TextChangedEventArgs e)
{
//ChangeFlag = true;
ButtonEnableCheck();
}
private void txtNoteLine_TextChanged(object sender, TextChangedEventArgs e)
{
//string Test = "";
GridPosition lTmpPOS;
GridPosition lCurPOS = new GridPosition();
int loc_col = 0;
int loc_max = 60;
WordWrap lWW = new WordWrap();
TextBox lTB = (TextBox)sender;
lCurPOS.RowNumber = grdNoteBody.Items.IndexOf(grdNoteBody.CurrentItem);
//lCurPOS.RowNumber = gvBillBody.Items.IndexOf(gvBillBody.CurrentItem);
lCurPOS.ColumnNumber = loc_col;
lCurPOS.CellCursorPosition = lTB.SelectionStart;
if (lCurPOS.CellCursorPosition > 0)
{
lTmpPOS = lWW.WrapBillBodyCol(
ref NoteBodyLines, ref grdNoteBody, ref lTB, loc_col, loc_max);
if (lTmpPOS != null)
{
string LogText = "";
LogText +=
"WW: row=" + lTmpPOS.RowNumber.ToString();
LogText +=
"; col=" + lTmpPOS.ColumnNumber.ToString();
LogText +=
"; celPOS=" + lTmpPOS.CellCursorPosition.ToString();
LogText +=
"\r\n";
_NewBillBodyPOS = lTmpPOS;
grdNoteBody.CommitEdit();
grdNoteBody.SelectedItems.Clear();
grdNoteBody.SelectedItems.Add(grdNoteBody.Items[lTmpPOS.RowNumber]);
grdNoteBody.SelectedItem = grdNoteBody.SelectedItems[0];
grdNoteBody.CurrentItem = NoteBodyLines[lTmpPOS.RowNumber];
grdNoteBody.BeginEdit();
//((GridViewCell)grdNoteBody.CurrentCell.ParentRow.Cells[lTmpPOS.ColumnNumber]).IsCurrent = true;
((
GridViewCell)grdNoteBody.CurrentCell).IsCurrent = true;
//grdNoteBody.BeginEdit();
}
}
}
private void txtNoteLine_KeyDown(object sender, KeyEventArgs e)
{
TextBox lTB = (sender as TextBox);
int lMaxLength = 60;
int lCurROW = 0;
switch (e.Key)
{
case Key.Enter:
lTB.Text = lTB.Text.PadRight(lMaxLength);
lCurROW = grdNoteBody.Items.IndexOf(grdNoteBody.CurrentItem);
//lCurROW = gvBillBody.Items.IndexOf(gvBillBody.CurrentItem);
grdNoteBody.CommitEdit();
if (grdNoteBody.CurrentCell.IsInEditMode == true)
return;
int lNewIndex = lCurROW + 1;
NoteBodyLines.Insert(lNewIndex,
new SLScreen.NoteSVC.TshipNoteBody());
//NoteBodyLines.Insert(lNewIndex, new CarotrackWeb.BillBody.BookingWCFsvc.BillBody());
_NewBillBodyPOS =
new GridPosition();
_NewBillBodyPOS.RowNumber = lNewIndex;
_NewBillBodyPOS.ColumnNumber = 0;
_NewBillBodyPOS.CellCursorPosition = 0;
grdNoteBody.SelectedItems.Clear();
grdNoteBody.SelectedItems.Add(grdNoteBody.Items[_NewBillBodyPOS.RowNumber]);
grdNoteBody.SelectedItem = grdNoteBody.SelectedItems[0];
grdNoteBody.CurrentItem = NoteBodyLines[_NewBillBodyPOS.RowNumber];
grdNoteBody.BeginEdit();
((
GridViewCell)grdNoteBody.CurrentCell.ParentRow.Cells[_NewBillBodyPOS.ColumnNumber]).IsCurrent = true;
grdNoteBody.BeginEdit();
break;
case Key.Back:
//if backspace then check to see if there is another
//row above that is part of the same section and
//put the cursor in that row at the end of the text
WordWrap lWW = new WordWrap();
lCurROW = grdNoteBody.Items.IndexOf(grdNoteBody.CurrentItem);
SLScreen.NoteSVC.
TshipNoteBody lBB = grdNoteBody.CurrentItem as SLScreen.NoteSVC.TshipNoteBody;
List<string> lColsToOmitFromDataCheck = new List<string>();
lColsToOmitFromDataCheck.Add(
"Description");
lColsToOmitFromDataCheck.Add(
"Marks");
if (lTB.SelectionStart == 0 && lCurROW > 0 && lWW.RowHasOtherData<SLScreen.NoteSVC.TshipNoteBody>(lBB, lColsToOmitFromDataCheck) == false)
{
grdNoteBody.CommitEdit();
if (grdNoteBody.CurrentCell.IsInEditMode == true)
return;
_NewBillBodyPOS =
new GridPosition();
_NewBillBodyPOS.RowNumber = lCurROW - 1;
_NewBillBodyPOS.ColumnNumber = 0;
NoteBodyLines[_NewBillBodyPOS.RowNumber].NoteLine = NoteBodyLines[_NewBillBodyPOS.RowNumber].NoteLine.Trim();
_NewBillBodyPOS.CellCursorPosition = NoteBodyLines[_NewBillBodyPOS.RowNumber].NoteLine.Length;
grdNoteBody.SelectedItems.Clear();
grdNoteBody.SelectedItems.Add(grdNoteBody.Items[_NewBillBodyPOS.RowNumber]);
grdNoteBody.SelectedItem = grdNoteBody.SelectedItems[0];
grdNoteBody.CurrentItem = NoteBodyLines[_NewBillBodyPOS.RowNumber];
grdNoteBody.BeginEdit();
((
GridViewCell)grdNoteBody.CurrentCell.ParentRow.Cells[_NewBillBodyPOS.ColumnNumber]).IsCurrent = true;
grdNoteBody.BeginEdit();
}
break;
default:
break;
}
}
private void txtNoteLine_Loaded(object sender, RoutedEventArgs e)
{
TextBox lTB = (sender as TextBox);
int lCurRow = grdNoteBody.Items.IndexOf(grdNoteBody.CurrentItem);
int lSelRow = grdNoteBody.Items.IndexOf(grdNoteBody.SelectedItem);
if (_NewBillBodyPOS != null && lSelRow == _NewBillBodyPOS.RowNumber)
{
string LogText = "";
LogText +=
"TBL: row=" + _NewBillBodyPOS.RowNumber.ToString();
LogText +=
"; col=" + _NewBillBodyPOS.ColumnNumber.ToString();
LogText +=
"; celPOS=" + _NewBillBodyPOS.CellCursorPosition.ToString();
LogText +=
"\r\n";
if (grdNoteBody.SelectedItem != grdNoteBody.CurrentItem)
{
grdNoteBody.CommitEdit();
grdNoteBody.SelectedItems.Clear();
grdNoteBody.SelectedItems.Add(NoteBodyLines[_NewBillBodyPOS.RowNumber]);
grdNoteBody.CurrentItem = NoteBodyLines[_NewBillBodyPOS.RowNumber];
grdNoteBody.BeginEdit();
((
GridViewCell)grdNoteBody.CurrentCell.ParentRow.Cells[_NewBillBodyPOS.ColumnNumber]).IsCurrent = true;
grdNoteBody.BeginEdit();
}
lTB.Focus();
lTB.SelectionStart = _NewBillBodyPOS.CellCursorPosition;
}
lTB.SelectionLength = 0;
}
private void grdNoteBody_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
{
e.NewObject =
new SLScreen.NoteSVC.TshipNoteBody();
}
//Misc Functions
private void ResetControls()
{
try
{
//reset text boxes to blank
txtTitle.Text =
"";
txtInvNum.Text =
"";
txtLastUpdate.Text =
"";
txtLastUser.Text =
"";
//reset listbox and combos
lstNotes.SelectedItem =
null;
grdNoteBody.ItemsSource =
null;
tddNoteClass.SelectedItem =
null;
tddNoteSec.SelectedItem =
null;
//reset buttons
btnAddNote.IsEnabled =
false;
btnDelete.IsEnabled =
false;
btnReset.IsEnabled =
false;
btnSaveEdit.IsEnabled =
false;
}
catch (Exception e)
{
throw;
}
}
private void ButtonEnableCheck()
{
string TitleText = txtTitle.Text;
string ClassText = tddNoteClass.Text;
string SecText = tddNoteSec.Text;
if (TitleText.Length > 0 && ClassText != null && SecText != null)
{
//Enable buttons
btnAddNote.IsEnabled =
true;
btnSaveEdit.IsEnabled =
true;
}
else
{
//Disable Buttons
btnAddNote.IsEnabled =
false;
btnSaveEdit.IsEnabled =
false;
}
if (TitleText.Length > 0 || ClassText.Length > 0 || SecText.Length > 0)
{
//Enable Reset Button
btnReset.IsEnabled =
true;
}
else
{
//Disable Reset Button
btnReset.IsEnabled =
false;
}
if (txtLastUpdate.Text.Length == 0)
btnSaveEdit.IsEnabled =
false;
}
private void PopulateControls(NoteSVC.TShipNoteType Note)
{
txtTitle.Text = Note.NoteTitle;
txtInvNum.Text = Note.InvNum.ToString();
tddNoteSec.SelectedIndex = Note.NoteLevel;
tddNoteClass.Text = Note.NoteClass.ToString();
txtLastUpdate.Text = Note.LastUpdate.ToString();
txtLastUser.Text = Note.LastUser.ToString();
NoteBodyLines = Note.NoteBodyLines;
NoteBodyLines.Add(
new NoteSVC.TshipNoteBody());
grdNoteBody.ItemsSource = NoteBodyLines;
//Turn on Buttons
btnAddNote.IsEnabled =
true;
btnDelete.IsEnabled =
true;
btnReset.IsEnabled =
true;
//grdNoteBody.BeginInsert();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
//public ObservableCollection<NoteSVC.TshipNoteBody> NoteBodyLines;
//private ObservableCollection<Player> data = new ObservableCollection<Player>();
//ObservableCollection<SLScreen.NoteSVC.TshipNoteBody> addnew = new ObservableCollection<SLScreen.NoteSVC.TshipNoteBody>();
//NoteBodyLines.Add("");
// var newData = new Player("dynamic", 0, Position.DF, "dynamic");
// this.data.Add(newData);
}
}
}
To me it's very confusing alot of collections ect..
I hope someone can make sense out of it.
Thanks,
Keith.