在运行时添加行的Gridview中,下拉列表未绑定. [英] Dropdown List is not getting binded in a Gridview where rows are added at Runtime.

查看:72
本文介绍了在运行时添加行的Gridview中,下拉列表未绑定.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海伊前辈..

我创建了一个网格视图,如下所示.

Haii Seniors..

i have Created a gird view as follows..

<

<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

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();

   }




并在gridview的Row databound事件中初始化下拉列表ddlField.




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

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..

推荐答案



如果拥有运行时数据,则需要在页面初始化时绑定数据.

因为一旦页面回发,所有在运行时生成的数据都将丢失.因此,对于每次回发,都应再次绑定运行时数据.

因此,在您的情况下,您需要在PageInitialization处调用SetInitialRow.

谢谢
-Amit.
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.


Haii Friends ..我必须使用

Haii Friends.. I have to use

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........


这篇关于在运行时添加行的Gridview中,下拉列表未绑定.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆