Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Haii Seniors..

i have Created a gird view as follows..
ASP.NET
<
<asp:GridView ID="dgrGenQry" runat="server" AutoGenerateColumns="False" 
                        AllowSorting="True" PageSize="1" onrowdatabound="dgrGenQry_RowDataBound" 
                        onselectedindexchanged="dgrGenQry_SelectedIndexChanged" >
                        <Columns>
                            <asp:TemplateField HeaderText="Field Name">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlField" runat="server" AppendDataBoundItems="True" 
                                        AutoPostBack="True" Width="140px" 
                                        onselectedindexchanged="ddlField_SelectedIndexChanged">
                                        <asp:ListItem Value="0">Select</asp:ListItem>
                                    </asp:DropDownList>
                                </ItemTemplate>
                                <ControlStyle Font-Bold="True" Font-Names="Arial Black" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Operator">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlOprtr" runat="server" AppendDataBoundItems="True" 
                                        AutoPostBack="True" Width="140px" 
                                        onselectedindexchanged="ddlOprtr_SelectedIndexChanged">
                                        
                                    </asp:DropDownList>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Value">
                                <ItemTemplate>
                                    <asp:TextBox ID="txtValue" runat="server"></asp:TextBox>
                                    <asp:DropDownList ID="ddlValue" runat="server" 
                                        onselectedindexchanged="ddlValue_SelectedIndexChanged" Visible="False">
                                    </asp:DropDownList>
                                    <asp:Calendar ID="calValue" runat="server" Visible="False"></asp:Calendar>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="AND/OR">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlAndOr"  runat="server" AutoPostBack="True" 
                                        Width="140px"  önselectedindexchanged="ddlAndOr_SelectedIndexChanged">
                                        <asp:ListItem Value="0">Select</asp:ListItem>
                                        <asp:ListItem>AND</asp:ListItem>
                                        <asp:ListItem>OR</asp:ListItem>
                                    </asp:DropDownList>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

and am initializing this in the Page load Event with the Following Function

C#
private void SetInitialRow()
   {
       DataTable dt = new DataTable();
       DataRow dr = null;
       dt.Columns.Add(new DataColumn("Field Name", typeof(string)));
       dt.Columns.Add(new DataColumn("Operator", typeof(string)));
       dt.Columns.Add(new DataColumn("Value", typeof(string)));
       dt.Columns.Add(new DataColumn("AND/OR", typeof(string)));
       dr = dt.NewRow();
       //string[] a = { "Select", "Equal to", "Greater Than", "Less Than", "Contains", "Like" };
       dr["Field Name"] = "Anish";
       dr["Operator"] = "Anish";
       dr["Operator"] = "Anish";
       dr["Value"] = "Anish";
       dr["AND/OR"] = "Anish";
       dt.Rows.Add(dr);
       //dr = dt.NewRow();
       //Store the DataTable in ViewState
       ViewState["CurrentTable"] = dt;

       dgrGenQry.DataSource = dt;
       dgrGenQry.DataBind();

   }



and in the Row databound Event of the gridview am initializing the dropdown list ddlField.

C#
if (e.Row.RowType != DataControlRowType.Header )
            {
                    DropDownList dl = (DropDownList)dgrGenQry.Rows[e.Row.RowIndex].Cells[0].FindControl("ddlField");
                    dl.DataSource = DT;
                    dl.DataTextField = "SearchLblName";
                    dl.DataValueField = "SearchFldType";
                    dl.DataBind();
                    //lblTagLink.CommandArgument = e.Row.RowIndex.ToString();
                
            } 

Here am getting an Error "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"
how can i Overcome this.. i want to initialize it in the first row creation itself..




Regards..
Posted
Updated 8-Apr-12 19:35pm
v3

Hi,

When you have runtime data, you need to bind your data at the time of Page Initialization.

Because once page is postback all the data that generated at runtime will be lost. So for each postback your runtime data should be bound again.

So in your case you need to call your SetInitialRow at PageInitialization.

thanks
-Amit.
 
Share this answer
 
Comments
anish.karunakaran 9-Apr-12 1:41am    
Haii.. I tried.. Bt the Same condition exists.. Am restoring the field values in every post back.. My task is to Initialize a grid row with sufficient data then based on user input Creating next row in the Grid View. next row is created by calling a function in the selected index changed event of ddlAndOr dropdown List. Thank You..
AmitGajjar 9-Apr-12 1:44am    
Ok, in that case you need to store your dropdown list information in some ViewState (initial value + Newly added value) and Bound viewstate value with your dropdown. currently what exact behavior you seen ?
anish.karunakaran 9-Apr-12 2:12am    
What the result i got after Calling SetInitialRow() is a Gridview with one row. But the dropdown list is not initialized with the data specified in the Row databound event of the Gridview.. The ddlField will get initialised whent i create the next row bt that created row's ddlField is not bound with Values. thank You Staying with..
Haii Friends.. I have to use

C#
DropDownList dl = (DropDownList)e.Row.FindControl("ddlField");
dl.DataSource = DT;
.
.
.
.

DropDownList dl = (DropDownList)dgrGenQry.Rows[e.Row.RowIndex].Cells[0].FindControl("ddlField");
dl.DataSource = DT;


Thank you........
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900