Editable DataGrid with DropDownList Question

F

Frank

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
 
G

Guest

Hi Frank,

Only when handling boundcolumn, TableCell.Text is used to retrieve data. To
handle TemplateColumn, specified control need to be used, e.g.

Label lbl = e.Item.FindControl("Label4") as Label;
string Accounts = lbl.Text;

HTH

Elton Wang

Frank said:
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
 

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

Top