Templated Field in an Editable GridView Problem

F

Frank

Hello All,

I am working with VS.NET 2005 and I have an editable GridView whose HTML
markup is shown below.

In short, when the user clicks on the Edit button, one of the textboxes is
replaced with a DropDownList that displays account numbers in a list. If the
user selects an account from the DropDownList that already exists in the
list, I wish to hide the 'Update' button or column, along with displaying a
warning message. I try to do this with this code behind:


protected void CheckForDup(object sender, EventArgs e)

{

int int_temp =
Convert.ToInt32(listAdapter.FindAccountInList(DDL_BlockList.SelectedValue,
((DropDownList)sender).SelectedValue));

if (int_temp != 0)

{

GridView Temp = new GridView();

Temp = (GridView)Page.FindControl("GV_Accts");

Temp.Columns[0].Visible = false;


LBL_Warn.Text = "That Account Already Exists in the List!";

LBL_Warn.Visible = true;

}

else

{

LBL_Warn.Visible = false;

}

}



However, when it runs, I get an "Object reference not set to an instance of
an object." exception.

It doesn't like the line that reads 'Temp.Columns[0].Visible = false;'

Anyone have any suggestions please?

Thanks in advance





<asp:GridView ID="GV_Accts" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID"

DataSourceID="ObjectDataSource2" OnRowUpdated="AdjustUnitDisplay"
OnRowDeleted="AdjustUnitDisplay" AllowPaging="True">

<Columns>

<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

<asp:TemplateField HeaderText="Account" SortExpression="Account">

<EditItemTemplate>

<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="DDL_Accts_DataSource"

DataTextField="Account" DataValueField="Account" AutoPostBack="True"
SelectedValue='<%# Bind("Account") %>' OnSelectedIndexChanged="CheckForDup"
Width="65px">

</asp:DropDownList>

<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="DDL_Lname_DataSource"

DataTextField="Lname" DataValueField="Lname" Width="90px">

</asp:DropDownList>&nbsp;

<asp:ObjectDataSource ID="DDL_Lname_DataSource" runat="server"

InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetLnameByAccount"

TypeName="Desk_A_ServerTableAdapters.CustomerTableAdapter">

<SelectParameters>

<asp:ControlParameter ControlID="DropDownList1" Name="Account"
PropertyName="SelectedValue"

Type="String" />

</SelectParameters>

<InsertParameters>

<asp:parameter Name="Account" Type="String" />

<asp:parameter Name="broker" Type="String" />

<asp:parameter Name="key" Type="String" />

<asp:parameter Name="Fname" Type="String" />

<asp:parameter Name="Lname" Type="String" />

</InsertParameters>

</asp:ObjectDataSource>

<asp:ObjectDataSource ID="DDL_Accts_DataSource" runat="server"

InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetCustomerByBrokerID"

TypeName="Desk_A_ServerTableAdapters.CustomerTableAdapter">

<SelectParameters>

<asp:SessionParameter Name="broker" SessionField="IB_Acct" Type="String" />

</SelectParameters>

<InsertParameters>

<asp:parameter Name="Account" Type="String" />

<asp:parameter Name="broker" Type="String" />

<asp:parameter Name="key" Type="String" />

<asp:parameter Name="Fname" Type="String" />

<asp:parameter Name="Lname" Type="String" />

</InsertParameters>

</asp:ObjectDataSource>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label1" runat="server" Text='<%# Bind("Account")
%>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="# of Contracts" SortExpression="value">

<EditItemTemplate>

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("value") %>'
Width="50px"></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label2" runat="server" Text='<%# Bind("value")
%>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Type" SortExpression="type">

<EditItemTemplate>

<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("type") %>'
Width="50px"></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label3" runat="server" Text='<%# Bind("type")
%>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top