473,379 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Using FindControl on editable datagrid

....I can't seem to get my hands on a control I'm loading in an editable
datagrid.

Here's my datagrid control:

<asp:datagrid
id="GLRulesGrid"
runat="server"
autogeneratecolumns="False"
oneditcommand="GLRulesGrid_Edit"
ondeletecommand="GLRulesGrid_Delete"
onupdatecommand="GLRulesGrid_Update"
oncancelcommand="GLRulesGrid_Cancel"
cssclass="embedded">
<headerstyle cssclass="large-title-red"></headerstyle>
<columns>
<asp:editcommandcolumn buttontype="LinkButton" edittext="Edit"
updatetext="Save" canceltext="Cancel" />
<asp:templatecolumn headertext="Transaction Type">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "TransactionType") %>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist
id="TransTypeList"
onload="TransTypeList_Load"
datasource="<%# TransTypes %>"
runat="server" />
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="Description">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</itemtemplate>
<edititemtemplate>
<asp:textbox
id="DescBox"
runat="server"
text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
</edititemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

In the dropdown list in the first column...I'm simply loading this
collection in the code behind class:

protected IList TransTypes
{
get
{
IList tTypes = new ArrayList();
tTypes.Add("RECV");
tTypes.Add("ADJUST");
tTypes.Add("JENSEN");
tTypes.Add("VAP");
tTypes.Add("SAMPLE");
tTypes.Add("SENT");

return tTypes;
}
}

....which works just fine...but I want to be able to select the current value
in the datagrid cell in the DropDownList control so the user doesn't
accidentally update the record w/ the wrong value, which would be the default
selected item in the DDL.

You'll see that I tried to use the Load event for the DDL but I'm still
unable to use the FindControl method in the code behind to get ahold of the
dropdownlist.

How can I achieve this?

Thanks!
Nov 19 '05 #1
2 5019
zambizzi,

You to find the Dropdown control in Itemdatabound event of the grid and
set the selected value of the dropdrop from the one found in your dataitem

Example in your Itemboundevent add this condition

if (itemType == ListItemType.EditItem)
{
DataRowView objDataRowView = (DataRowView) e.Item.DataItem;

if (objDataRowView["TransactionType"] != null && objDataRowView
["TransactionType"] !=System.DBNull.Value)
{
DropDownList cboTransType =
(DropDownList)e.Item.Cells[2].FindControl
("TransTypeList");
cboTransType.Items.FindByText((string)objDataRowVi ew
["TransactionType"]).Selected = true;
cboTransType.Items.FindByText(objDataRowView[").Selected=true;;
}

}

In this way the dropdownlist will show the values selected which was there
in the itemtemplate.

Note :No need for onload="TransTypeList_Load"

Hope this thing works for you

Regards

IntelYogi
"zambizzi" wrote:
...I can't seem to get my hands on a control I'm loading in an editable
datagrid.

Here's my datagrid control:

<asp:datagrid
id="GLRulesGrid"
runat="server"
autogeneratecolumns="False"
oneditcommand="GLRulesGrid_Edit"
ondeletecommand="GLRulesGrid_Delete"
onupdatecommand="GLRulesGrid_Update"
oncancelcommand="GLRulesGrid_Cancel"
cssclass="embedded">
<headerstyle cssclass="large-title-red"></headerstyle>
<columns>
<asp:editcommandcolumn buttontype="LinkButton" edittext="Edit"
updatetext="Save" canceltext="Cancel" />
<asp:templatecolumn headertext="Transaction Type">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "TransactionType") %>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist
id="TransTypeList"
onload="TransTypeList_Load"
datasource="<%# TransTypes %>"
runat="server" />
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="Description">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</itemtemplate>
<edititemtemplate>
<asp:textbox
id="DescBox"
runat="server"
text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
</edititemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

In the dropdown list in the first column...I'm simply loading this
collection in the code behind class:

protected IList TransTypes
{
get
{
IList tTypes = new ArrayList();
tTypes.Add("RECV");
tTypes.Add("ADJUST");
tTypes.Add("JENSEN");
tTypes.Add("VAP");
tTypes.Add("SAMPLE");
tTypes.Add("SENT");

return tTypes;
}
}

...which works just fine...but I want to be able to select the current value
in the datagrid cell in the DropDownList control so the user doesn't
accidentally update the record w/ the wrong value, which would be the default
selected item in the DDL.

You'll see that I tried to use the Load event for the DDL but I'm still
unable to use the FindControl method in the code behind to get ahold of the
dropdownlist.

How can I achieve this?

Thanks!

Nov 19 '05 #2
OK, with a little poking and prodding of your example I got it to *almost*
work. I can now get a grip on the drop down list but am unable to select the
item w/ the current dataitem value...here's my event handler for the datagrid
OnItemDataBound event.

protected void GLRulesGrid_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;

if (itemType == ListItemType.EditItem)
{
//set row editable
if (e.Item.DataItem != null)
{
IGLRule rowRule = (IGLRule)e.Item.DataItem;

DropDownList transTypes =
(DropDownList)e.Item.Cells[1].FindControl("TransTypeList");
transTypes.Items.FindByValue(rowRule.TransactionTy pe).Selected = true;
}
}
}

....I'm using an object model to represent data....that's what the type
"IGLRule" is...FYI. I walked through it in the debugger and the data is
working fine...I can read all of my values....including the "TransactionType"
property of the rowRule variable (which is a business object with a type of
IGLRule.) it fails on the line where I try to the ddl Selected to
true....the last line.

I get this error:

"Object reference not set to an instance of an object."

....which doens't make sense....I can read from my business object's
properties AND from the dropdownlist declared one line before the one that
fails....why can't I select it?

Thanks!

"IntelYogi" wrote:
zambizzi,

You to find the Dropdown control in Itemdatabound event of the grid and
set the selected value of the dropdrop from the one found in your dataitem

Example in your Itemboundevent add this condition

if (itemType == ListItemType.EditItem)
{
DataRowView objDataRowView = (DataRowView) e.Item.DataItem;

if (objDataRowView["TransactionType"] != null && objDataRowView
["TransactionType"] !=System.DBNull.Value)
{
DropDownList cboTransType =
(DropDownList)e.Item.Cells[2].FindControl
("TransTypeList");
cboTransType.Items.FindByText((string)objDataRowVi ew
["TransactionType"]).Selected = true;
cboTransType.Items.FindByText(objDataRowView[").Selected=true;;
}

}

In this way the dropdownlist will show the values selected which was there
in the itemtemplate.

Note :No need for onload="TransTypeList_Load"

Hope this thing works for you

Regards

IntelYogi
"zambizzi" wrote:
...I can't seem to get my hands on a control I'm loading in an editable
datagrid.

Here's my datagrid control:

<asp:datagrid
id="GLRulesGrid"
runat="server"
autogeneratecolumns="False"
oneditcommand="GLRulesGrid_Edit"
ondeletecommand="GLRulesGrid_Delete"
onupdatecommand="GLRulesGrid_Update"
oncancelcommand="GLRulesGrid_Cancel"
cssclass="embedded">
<headerstyle cssclass="large-title-red"></headerstyle>
<columns>
<asp:editcommandcolumn buttontype="LinkButton" edittext="Edit"
updatetext="Save" canceltext="Cancel" />
<asp:templatecolumn headertext="Transaction Type">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "TransactionType") %>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist
id="TransTypeList"
onload="TransTypeList_Load"
datasource="<%# TransTypes %>"
runat="server" />
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="Description">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
</itemtemplate>
<edititemtemplate>
<asp:textbox
id="DescBox"
runat="server"
text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' />
</edititemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>

In the dropdown list in the first column...I'm simply loading this
collection in the code behind class:

protected IList TransTypes
{
get
{
IList tTypes = new ArrayList();
tTypes.Add("RECV");
tTypes.Add("ADJUST");
tTypes.Add("JENSEN");
tTypes.Add("VAP");
tTypes.Add("SAMPLE");
tTypes.Add("SENT");

return tTypes;
}
}

...which works just fine...but I want to be able to select the current value
in the datagrid cell in the DropDownList control so the user doesn't
accidentally update the record w/ the wrong value, which would be the default
selected item in the DDL.

You'll see that I tried to use the Load event for the DDL but I'm still
unable to use the FindControl method in the code behind to get ahold of the
dropdownlist.

How can I achieve this?

Thanks!

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andla Rand | last post by:
Hi, Could you help me to understand how datagrid works. I'm trying to make a datagrid editable. Now most of the tutorials I read says that i should turn the Enable ViewState off. Why is that? ...
1
by: mai via DotNetMonster.com | last post by:
hi, i'm sort of a newbie in .net and i'm currently doing my major project. i would like to know if it is possible to have a datagrid that would not be having a database as its datasource? and is it...
4
by: Stephan Bour | last post by:
Hi, I have a datagrid databound to a SQL query. I'd like to allow editing of some columns but not all. Is there a way to turn off the conversion of the datagrid cells to textboxes for some columns...
2
by: Jesper Stocholm | last post by:
I have a challenge, that I do not seem to be able to solve. I have an "overview"-page that presently displays data from 2 columns in a database table. The users should be allowed to edit data in...
0
by: DaveR | last post by:
I have a webform with a two-column datagrid based on an Arraylist. The Arraylist draws the data for the two columns from two different tables in an SQL database. The data is displayed in datagrid...
5
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated...
7
by: Diane | last post by:
Hi- I've been struggling with this problem and none of the fixes posted seem to help out at all. Yet, it seems like such a simple problem... I have a DataGrid, and load it up with data on...
5
by: Rajesh | last post by:
Hi, Please help me to do an EXCEL report generation using ASP.NET. I am building an website. This should work in all machine. I need do load the data from SQL tables to excel sheet and user can...
2
by: toddw607 | last post by:
Hi all! I am attempting to bring an SQL Server table into ASP.NET using the datagrid . I have set all cells to be a text box by which the user can just click on and edit the cell. I have a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.