Friday

ASP.NET Editable grid

aspx page:
 
<div style="overflow:auto;height:150px;border-width:1px;">
<asp:DataGrid ID="ScheduledPaymentAdjustmentDataGrid" CssClass="TableWithGrayBorders" Runat="server"
AutoGenerateColumns="False"
Width="200" BorderStyle="None"
HeaderStyle-CssClass="TableWithGrayBordersHeader" ShowFooter=true
HeaderStyle-HorizontalAlign="Center" DataKeyField="sID"
OnItemCommand="ItemsGrid_Command"
>

<Columns>
<asp:TemplateColumn HeaderText="Profile" ItemStyle-Width="5%" ItemStyle-Wrap=False
HeaderStyle-HorizontalAlign=Center HeaderStyle-Wrap=False ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<img src="../images/spacer.gif" width=2>
<asp:DropDownList id="ProfileDropDown" runat="server" Width='150'
DataSource="<%# GetProfiles() %>"
DataValueField="id" DataTextField="name"
SelectedValue='<%# GetProFileId(DataBinder.Eval(Container.DataItem, "sID")) %>'
/>
<img src="../images/spacer.gif" width=2>
</ItemTemplate>

<FooterTemplate>

<img src="../images/spacer.gif" width=2>
<asp:DropDownList id="ProfileDropDown" runat="server" Width='150' BackColor="LightGreen"
DataSource="<%# GetProfiles() %>"
DataValueField="id" DataTextField="name"
SelectedValue='<%# GetProFileId(DataBinder.Eval(Container.DataItem, "sID")) %>'
/>
<img src="../images/spacer.gif" width=2>
</FooterTemplate>
</asp:TemplateColumn>


<asp:TemplateColumn HeaderText="Amount" ItemStyle-Width="5%" ItemStyle-Wrap=False
HeaderStyle-HorizontalAlign=Center HeaderStyle-Wrap=False ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<img src="../images/spacer.gif" width=2>
<asp:TextBox ID="AmountTextBox" runat="server" Text='<%# string.Format("{0:0.00}",DataBinder.Eval(Container.DataItem, "dAmount")) %>' Width='70' MaxLength="8" />
<img src="../images/spacer.gif" width=2>
</ItemTemplate>

<FooterTemplate>
<img src="../images/spacer.gif" width=2>
<asp:TextBox ID="AmountTextBox" runat="server" BackColor="LightGreen" Text='<%# string.Format("{0:0.00}",DataBinder.Eval(Container.DataItem, "dAmount")) %>' Width='70' MaxLength="8" />
<img src="../images/spacer.gif" width=2>
</FooterTemplate>

</asp:TemplateColumn>


<asp:TemplateColumn HeaderText="" ItemStyle-Width="1%" ItemStyle-Wrap=False
HeaderStyle-HorizontalAlign=Center HeaderStyle-Wrap=False ItemStyle-HorizontalAlign=Center>
<ItemTemplate>

<asp:ImageButton ID="ImageButton1" Runat="server" CommandName="Delete" ImageUrl ='../images/delete_icon.gif' CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID")%>' />

</ItemTemplate>

<FooterTemplate>
<asp:ImageButton ID="ImageButton2" Runat="server" ImageUrl='../images/bullet_add.png' CommandName="new" />
</FooterTemplate>

</asp:TemplateColumn>
</Columns>

</asp:DataGrid>



Code behind:
  
public void SaveScheduledPaymentAdjustmentDataGridChanges()
{
List<InvoiceDetailsRowView> lst = ((List<InvoiceDetailsRowView>)ScheduledPaymentAdjustmentDataGridSource);
Hashtable H = new Hashtable(lst.Count);

bool changed = false;
foreach (InvoiceDetailsRowView i in lst)
{
H.Add(i.sID,i);

}


foreach (DataGridItem dataGridItem in ScheduledPaymentAdjustmentDataGrid.Items)
{
string key =(string) ScheduledPaymentAdjustmentDataGrid.DataKeys[dataGridItem.ItemIndex];
UpdateInvoiceDetailsRowViewWithCells((InvoiceDetailsRowView)H[key], dataGridItem.Cells);
}

ScheduledPaymentAdjustmentDataGridSource = lst;
}



public void UpdateInvoiceDetailsRowViewWithCells(InvoiceDetailsRowView i,TableCellCollection Cells )
{
TextBox date = (TextBox)Cells[2].FindControl("DateTextBox");
TextBox Note = (TextBox)Cells[3].FindControl("NoteTextBox");
TextBox amount = (TextBox)Cells[1].FindControl("AmountTextBox");
DropDownList dd = (DropDownList)Cells[0].FindControl("ProfileDropDown");

decimal am;
assert.IsTrue(decimal.TryParse(amount.Text, out am), iam+" " +amount.Text);
assert.IsTrue(am > 0, iam + " " + amount.Text);
i.NoteText = Note.Text;
i.dAmount = am;
i.TXNDate = date.Text;
i.ProfileID = int.Parse(dd.SelectedValue);

}



public void ItemsGrid_Command(Object sender, DataGridCommandEventArgs e)
{

RunSafe(Process_Command,new object[]{e});
}

public void Process_Command(params object[] p)
{
DataGridCommandEventArgs e =(DataGridCommandEventArgs) p[0];

switch (e.CommandName)
{
case "Delete":
SaveScheduledPaymentAdjustmentDataGridChanges();
DeleteSP((string)ScheduledPaymentAdjustmentDataGrid.DataKeys[e.Item.ItemIndex]);
break;

case "new":
SaveScheduledPaymentAdjustmentDataGridChanges();

InvoiceDetailsRowView i= new InvoiceDetailsRowView();
UpdateInvoiceDetailsRowViewWithCells(i, ((System.Web.UI.WebControls.TableRow) (e.Item)).Cells);
i.status = EnumRecordStatus.added;
i.sID = Guid.NewGuid().ToString();
List<InvoiceDetailsRowView> lst = ((List<InvoiceDetailsRowView>)ScheduledPaymentAdjustmentDataGridSource);
lst.Add(i);
ScheduledPaymentAdjustmentDataGridSource = lst;
ScheduledPaymentAdjustmentDataGridBind();
break;
}
GetTotalInBills();
}


more...

No comments:

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...