ButtonColumn not firing after dynamically adding a row to my datagrid.

Joined
Oct 20, 2009
Messages
1
Reaction score
0
I have a fairly simple datagrid, and had to add some seperator rows. Once I did this I noticed that my delete column was acting a little funny. Upon further investigation I realized that it is only the last item in the datagraid that will not delete if there is only one header and then the wrong rows get deleted when there are more then one dynamic rows inserted.

The datagrid seems to be losing count of where it is and what its supposed to do once the new title rows are added. Has anyone ran into this? Here is some of my code --

<asp:DataGrid ID="dgAttachments"
CssClass="striped_table"
GridLines="None"
runat="server"
ShowHeader="true"
DataKeyField="attachmentid"
OnDeleteCommand="DeleteMyAttachment"
Width="100%"
AutoGenerateColumns="false"
OnItemDataBound="MyAttachmentsDataBound"
ItemStyle-CssClass="oddrow"
AlternatingItemStyle-CssClass="evenrow">

<HeaderStyle CssClass="headerStriped" />
<Columns>
<asp:HyperLinkColumn Target="_blank" HeaderText="File Name" DataNavigateUrlField="attachmentid" DataNavigateUrlFormatString="/aime/util/attachment.aspx?attachmentid={0}" DataTextField="original_file_name"></asp:HyperLinkColumn>
<asp:BoundColumn HeaderText="File Size" DataField="filesize"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Uploaded By" DataField="uploaded_by"></asp:BoundColumn>
<acntrl:DateColumn HeaderText="Uploaded Date" DataField="time_stamp" />
<asp:ButtonColumn CommandName="Delete" ButtonType="linkbutton" Text="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>



int intPrevDocLevel = 0;
int added = 1;

public void DeleteMyAttachment(object sender, DataGridCommandEventArgs e)
{
try
{
if (e.CommandName == "Delete")
{
AttachmentID = Util.forceInt(dgAttachments.DataKeys[e.Item.ItemIndex].ToString());

DeleteAttachment();

litULMessage.Text = "File Successfully Deleted";
}

}
catch (Exception x)
{
//Define your error here. If in a class, pass x.Message, if in a .aspx page, just pass x
AimeError newErr = new AimeError(x, 0, "DeleteMyAttachment", "AimeFileUpload.ascx");
}
}



public void MyAttachmentsDataBound(object sender, DataGridItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
System.Data.Common.DbDataRecord r = (System.Data.Common.DbDataRecord)e.Item.DataItem;

bool blnAdmin = Util.forceBool(r["attach_owner"].ToString());
int intAttachmentID = (int)r["attachmentid"];
DataGrid DG = (DataGrid)sender;


if (!blnAdmin)
{
e.Item.Cells[4].Text = "";
}

if (UploadType == 1 || UploadType == 2)
{
if (intPrevDocLevel != (int)r["doc_level"])
{
DataGridItem headerRow = new DataGridItem(e.Item.ItemIndex + added, e.Item.ItemIndex + added, ListItemType.Item);
headerRow.CssClass = "fixedSubTitle";

TableCell cellTitle = new TableCell();
cellTitle.Text = (int)r["doc_level"] == 1 ? "Current Attachments" : "Parent Attachments";
cellTitle.ColumnSpan = 5;
cellTitle.Style.Add("padding-left", "2px");
headerRow.Cells.Add(cellTitle);


DG.Controls[0].Controls.AddAt(e.Item.ItemIndex + added, headerRow);

intPrevDocLevel = (int)r["doc_level"];


added++;
}

}
}
}
catch (Exception x)
{
//Define your error here. If in a class, pass x.Message, if in a .aspx page, just pass x
AimeError newErr = new AimeError(x, 0, "OnDGDataBind", "discussion_pop.aspx.cs");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top