By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 2,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

Editable DataGrid with DropDownList Question

P: n/a
Hello,

I am developing with VS.Net 2003 on the asp 1.1 platform.

I am a few days new to using a datagrid. I found a nice tutorial and had no
problems using an editable datagrid with textboxes and an additional
buttoncolumn where I used a delete button to delete a specific row.

Once I had finished that, I trnsformed the grid tso it will display a
dropdownlist indtead of a textbox. After much grappling, I pretty much have
it working except for one detail. Now, the delete function will not work
properly.

Following is the declaration of the grid in the .aspx page:

<asp:datagrid id=DGR_List runat="server" cellpadding="3"
oneditcommand="DGR_List_Edit" oncancelcommand="DGR_List_Cancel"
onupdatecommand="DGR_List_Update" onitemcommand="DGR_List_Command"
onitemdatabound="DGR_List_ItemDataBound" autogeneratecolumns="false">

<edititemstyle forecolor="Maroon"></EditItemStyle>

<alternatingitemstyle backcolor="#B8C6D4"></AlternatingItemStyle>

<headerstyle font-bold="True" forecolor="White"
backcolor="#336598"></HeaderStyle>

<columns>

<asp:TemplateColumn HeaderText="Accounts">

<itemtemplate>

<asp:label id=Label4 runat="server" text='<%#
DataBinder.Eval(Container, "DataItem.Account") %>'><!--use container because
the DGR is bound to the conatiner--></asp:label>

</ItemTemplate>

<edititemtemplate>

<!--this is a different data source...not what the dgr is bound
to-->

<asp:dropdownlist onselectedindexchanged="Change_Cust_TB"
id=DropDownList1 runat="server"
DataSource="<%#bizObj.Get_IB_Accounts(IB_Acct)%>" DataTextField="Account"
DataValueField="Account" Width="100px" autopostback="True">
</asp:dropdownlist>

</EditItemTemplate>

</asp:TemplateColumn>

<asp:BoundColumn DataField="Value" ReadOnly="True"
HeaderText="Value"></asp:BoundColumn>

<asp:BoundColumn DataField="Unit" ReadOnly="True"
HeaderText="Units"></asp:BoundColumn>

<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Edit Acct" CancelText="Cancel" EditText="Edit">
<headerstyle wrap="False"></HeaderStyle>

<itemstyle wrap="False"></ItemStyle>

</asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" HeaderText="Delete Acct"
CommandName="Delete">

<headerstyle wrap="False"></HeaderStyle>

<itemstyle wrap="False"></ItemStyle>

</asp:ButtonColumn>

</Columns>

</asp:datagrid>

When the Delete button is clicked, the onitemcommand event fires and
DGR_List_Command is called:

public void DGR_List_Command(Object sender, DataGridCommandEventArgs e)

{

switch(((LinkButton)e.CommandSource).CommandName)

{

case "Delete":

Delete_Acct(e);

break;

default:

//do nothing

break;

}

}

And the function calls Delete_Acct(e):

public void Delete_Acct(DataGridCommandEventArgs e)

{

// e.Item is the table row where the command is raised. For bound

// columns, the value is stored in the Text property of a TableCell.

TableCell itemCell = e.Item.Cells[0];

string strCurrentAcct= itemCell.Text;

//call my own function to dlete item in first cell

bizObj.Delete_Acct(strListName, strCurrentAcct);

//do some more stuff and reload grid

Mar 9 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Frank,
Try

DropDownList ddk = (DropDownList) e.Item.Cells[X].FindControl("ddlId");

Another example:

TextBox tb = (TextBox)e.Item.Cells[5].FindControl("txUserId");

HTH
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Frank" wrote:
Hello,

I am developing with VS.Net 2003 on the asp 1.1 platform.

I am a few days new to using a datagrid. I found a nice tutorial and had no
problems using an editable datagrid with textboxes and an additional
buttoncolumn where I used a delete button to delete a specific row.

Once I had finished that, I trnsformed the grid tso it will display a
dropdownlist indtead of a textbox. After much grappling, I pretty much have
it working except for one detail. Now, the delete function will not work
properly.

Following is the declaration of the grid in the .aspx page:

<asp:datagrid id=DGR_List runat="server" cellpadding="3"
oneditcommand="DGR_List_Edit" oncancelcommand="DGR_List_Cancel"
onupdatecommand="DGR_List_Update" onitemcommand="DGR_List_Command"
onitemdatabound="DGR_List_ItemDataBound" autogeneratecolumns="false">

<edititemstyle forecolor="Maroon"></EditItemStyle>

<alternatingitemstyle backcolor="#B8C6D4"></AlternatingItemStyle>

<headerstyle font-bold="True" forecolor="White"
backcolor="#336598"></HeaderStyle>

<columns>

<asp:TemplateColumn HeaderText="Accounts">

<itemtemplate>

<asp:label id=Label4 runat="server" text='<%#
DataBinder.Eval(Container, "DataItem.Account") %>'><!--use container because
the DGR is bound to the conatiner--></asp:label>

</ItemTemplate>

<edititemtemplate>

<!--this is a different data source...not what the dgr is bound
to-->

<asp:dropdownlist onselectedindexchanged="Change_Cust_TB"
id=DropDownList1 runat="server"
DataSource="<%#bizObj.Get_IB_Accounts(IB_Acct)%>" DataTextField="Account"
DataValueField="Account" Width="100px" autopostback="True">
</asp:dropdownlist>

</EditItemTemplate>

</asp:TemplateColumn>

<asp:BoundColumn DataField="Value" ReadOnly="True"
HeaderText="Value"></asp:BoundColumn>

<asp:BoundColumn DataField="Unit" ReadOnly="True"
HeaderText="Units"></asp:BoundColumn>

<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Edit Acct" CancelText="Cancel" EditText="Edit">
<headerstyle wrap="False"></HeaderStyle>

<itemstyle wrap="False"></ItemStyle>

</asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" HeaderText="Delete Acct"
CommandName="Delete">

<headerstyle wrap="False"></HeaderStyle>

<itemstyle wrap="False"></ItemStyle>

</asp:ButtonColumn>

</Columns>

</asp:datagrid>

When the Delete button is clicked, the onitemcommand event fires and
DGR_List_Command is called:

public void DGR_List_Command(Object sender, DataGridCommandEventArgs e)

{

switch(((LinkButton)e.CommandSource).CommandName)

{

case "Delete":

Delete_Acct(e);

break;

default:

//do nothing

break;

}

}

And the function calls Delete_Acct(e):

public void Delete_Acct(DataGridCommandEventArgs e)

{

// e.Item is the table row where the command is raised. For bound

// columns, the value is stored in the Text property of a TableCell.

TableCell itemCell = e.Item.Cells[0];

string strCurrentAcct= itemCell.Text;

//call my own function to dlete item in first cell

bizObj.Delete_Acct(strListName, strCurrentAcct);

//do some more stuff and reload grid

.

.

LoadData();

}

}

This code worked fine until I added the dropdownlist. Now, in the
Delete_Acct function, I cannot get a value from the first cell, which is
where the droplist is.

I can get values into the variable strCiurrentAcct if I change TableCell
itemCell = e.Item.Cells[0];
to TableCell itemCell = e.Item.Cells[1];
or TableCell itemCell = e.Item.Cells[2];

these cells contain no text boxes nor droplists.

I tried doing a

if(e.Item.ItemType == ListItemType.EditItem)

{

// get value

}

but the this code doesn't get hit when I debug. The e.Item.ItemType is
recognised as a TableRow

Any suggestions?

Thanks in advance,

Frank

Mar 9 '06 #2

P: n/a
Peter,

Thanks for the reply. I appreciate your time. I tried your suggestion
but had no luck. I ended up splitting the task into two pages, where I
perform the delete functionality on another page. There, I simply create
another grid without the droplist, load it with data, and then allow
users to delete. I am sure there is a better way, but am short on time.
Thanks,

Frank

*** Sent via Developersdex http://www.developersdex.com ***
Mar 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.