Solved

dropdownlist in gridview selected item

Posted on 2014-02-18
1
429 Views
Last Modified: 2014-02-18
it is working good,  BUT..   when i change BoundField DataField="servdisk"  Visible to False it is stop  working?  how to solve?


     <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView3_RowDataBound" >
            <Columns>
                <asp:BoundField DataField="driveD" HeaderText=" Drive " />
                <asp:BoundField DataField="file_id" HeaderText="Order" />
                <asp:BoundField DataField="type" HeaderText="type" />
                <asp:BoundField DataField="current_file_location" HeaderText="Path" />
                <asp:BoundField DataField="size" HeaderText="File Size" />
                <asp:BoundField DataField="sizeAF" HeaderText="Size After" />
                <asp:BoundField DataField="servdisk" HeaderText="servdisk" Visible="True" />
                <asp:TemplateField HeaderText="Server Disk">
                    <ItemTemplate><asp:DropDownList ID="servedisk_new" runat="server" Width="100px"/></ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>





    protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var ddl = (DropDownList)e.Row.FindControl("servedisk_new");
            ddl.DataSource = GetServDriveSize(DestServDropD.SelectedItem.Text, 0, 0, 2); ;
            ddl.DataTextField = "DiskName";
            ddl.DataValueField = "DiskName";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("Choos Drive", "0"));

           string seltxt= Convert.ToString(e.Row.Cells[6].Text);


            ddl.SelectedValue = seltxt;
        }



    }
0
Comment
Question by:center1010
1 Comment
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 39868255
If you set Visible="False", the BoundField isn't rendered, so you can't find it.
There are many different ways for what you are trying to do:
1: Add a HiddenField to the same TemplateField as your dropdown. Find that field instead like:
HiddenField hf= e.Row.Find("HiddenField1") as HiddenField;
if(hf!=null)
   ddl.SelectedValue = hf.Value;

Open in new window

2: Another way is use DataKeys
3: You can try setting DropDownList's  SelectedValue='<%#Eval("servdisk")%>' in the markup.
4: You can get the value from DataItem in the RowDataBound like:
DataRowView drv = e.Row.DataItem as DataRowView;
ddl.SelectedValue = drv["servdisk"].ToString();

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
A short film showing how OnPage and Connectwise integration works.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now