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(obj ect sender, EventArgs e)
{
int int_temp =
Convert.ToInt32 (listAdapter.Fi ndAccountInList (DDL_BlockList. SelectedValue,
((DropDownList) sender).Selecte dValue));
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.Visibl e = true;
}
else
{
LBL_Warn.Visibl e = 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" AutoGenerateCol umns="False"
DataKeyNames="I D"
DataSourceID="O bjectDataSource 2" OnRowUpdated="A djustUnitDispla y"
OnRowDeleted="A djustUnitDispla y" AllowPaging="Tr ue">
<Columns>
<asp:CommandFie ld ShowDeleteButto n="True" ShowEditButton= "True" />
<asp:TemplateFi eld HeaderText="Acc ount" SortExpression= "Account">
<EditItemTempla te>
<asp:DropDownLi st ID="DropDownLis t1" runat="server"
DataSourceID="D DL_Accts_DataSo urce"
DataTextField=" Account" DataValueField= "Account" AutoPostBack="T rue"
SelectedValue=' <%# Bind("Account") %>' OnSelectedIndex Changed="CheckF orDup"
Width="65px">
</asp:DropDownLis t>
<asp:DropDownLi st ID="DropDownLis t2" runat="server"
DataSourceID="D DL_Lname_DataSo urce"
DataTextField=" Lname" DataValueField= "Lname" Width="90px">
</asp:DropDownLis t>
<asp:ObjectData Source ID="DDL_Lname_D ataSource" runat="server"
InsertMethod="I nsert" OldValuesParame terFormatString ="original_{ 0}"
SelectMethod="G etLnameByAccoun t"
TypeName="Desk_ A_ServerTableAd apters.Customer TableAdapter">
<SelectParamete rs>
<asp:ControlPar ameter ControlID="Drop DownList1" Name="Account"
PropertyName="S electedValue"
Type="String" />
</SelectParameter s>
<InsertParamete rs>
<asp:Paramete r Name="Account" Type="String" />
<asp:Paramete r Name="broker" Type="String" />
<asp:Paramete r Name="key" Type="String" />
<asp:Paramete r Name="Fname" Type="String" />
<asp:Paramete r Name="Lname" Type="String" />
</InsertParameter s>
</asp:ObjectDataS ource>
<asp:ObjectData Source ID="DDL_Accts_D ataSource" runat="server"
InsertMethod="I nsert" OldValuesParame terFormatString ="original_{ 0}"
SelectMethod="G etCustomerByBro kerID"
TypeName="Desk_ A_ServerTableAd apters.Customer TableAdapter">
<SelectParamete rs>
<asp:SessionPar ameter Name="broker" SessionField="I B_Acct" Type="String" />
</SelectParameter s>
<InsertParamete rs>
<asp:Paramete r Name="Account" Type="String" />
<asp:Paramete r Name="broker" Type="String" />
<asp:Paramete r Name="key" Type="String" />
<asp:Paramete r Name="Fname" Type="String" />
<asp:Paramete r Name="Lname" Type="String" />
</InsertParameter s>
</asp:ObjectDataS ource>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Account")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="# of Contracts" SortExpression= "value">
<EditItemTempla te>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("value") %>'
Width="50px"></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label2" runat="server" Text='<%# Bind("value")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Typ e" SortExpression= "type">
<EditItemTempla te>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("type") %>'
Width="50px"></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label3" runat="server" Text='<%# Bind("type")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
</Columns>
</asp:GridView>