How to access the Gridview controls from the user defined function ? [Resolved]

Posted by .Netlearner under ASP.NET on 6/5/2012 | Points: 10 | Views : 2238 | Status : [Member] | Replies : 2
i have developed a gridview in which i have a dropdownlist which must be loaded dynamically in the pageload event.I dont know how to mention the dropdownlist present within the gridview.
I tried as follows,
 protected void LoadShipping()
{
int index;
index = 0;

con.Open();
cmd = new SqlCommand("SC_SP_SelectShippingRates", con);
cmd.CommandType = CommandType.StoredProcedure;
dr = cmd.ExecuteReader();
while (dr.Read())
{
ddlshipping.Items.Add(dr["Shipping"].ToString());
ddlshipping.Items[index].Value = dr["ShippingID"].ToString();
index++;

}
con.Close();
}


but this causes an error that the ddlshipping doesnot exists in the current context.

Design is as follows:
<asp:GridView ID="gvcheckout" runat="server" AutoGenerateColumns="False" OnRowCommand="gvcheckout_RowCommand" OnRowUpdating="gvcheckout_RowUpdating" OnSelectedIndexChanging="gvcheckout_OnSelectedIndexChanging" OnRowDataBound="gvcheckout_RowDataBound"
CellPadding="3" CssClass="GridViewStyle" Width="100%" Height="350px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblShipping" runat="server" Text="Shipping" Width="100px" ></asp:Label>
<asp:DropDownList ID="ddlshipping" runat="server" AutoPostBack="true" Width="200px"></asp:DropDownList>

</ItemTemplate>
</asp:TemplateField>

</Columns>
<RowStyle CssClass="rowdata" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="gridheading" BackColor="#004d6a" ForeColor="white" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="rowdata1" BackColor="#F5F5F5" />
</asp:GridView>


how can i load dynaic values in the dropdownlist present in the grid ?
How can i change the properties of the controls present in the grid in user defined function ?




Responses

Posted by: Meet1424 on: 6/5/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved

protected void LoadShipping(DropDownList ddltobind)

{

int index;

index = 0;



con.Open();

cmd = new SqlCommand("SC_SP_SelectShippingRates", con);

cmd.CommandType = CommandType.StoredProcedure;

dr = cmd.ExecuteReader();

while (dr.Read())

{

ddltobind.Items.Add(dr["Shipping"].ToString());

ddltobind.Items[index].Value = dr["ShippingID"].ToString();

index++;



}

con.Close();

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

foreach (GridViewRow gRow in GridView1.Rows)
{

DropDownList ddlshipping= (DropDownList)gRow.FindControl("ddlshipping");

ddlshipping.Items.Clear();

LoadShipping(ddlshipping)

}
}

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: .Netlearner on: 6/5/2012 [Member] Starter | Points: 25

Up
0
Down
Thanku so much it worked.

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response