Home All Groups Group Topic Archive Search About

DropDownlist in EditItemTemplate Gridview Problem

Author
11 Oct 2006 11:34 AM
den 2005
Hi everybody,

   I created a Gridview with a TemplateField and there is Label control in
ItemTemplate and a DropdownList control in EditItemTemplate, I was to
displayed them ok when I click the Edit Command button. But when I about to
get the value of this template field I get an empty string value. I check the
following the local dtFiles has data and the cells are visible at stage right
before foreach GridViewRow gr in gdvFiles.Rows) loop. Can anyone tell what is
missing or I went wrong? Thanks

[code]
            <asp:GridView ID="gdvFiles" runat="server"
AutoGenerateColumns="False" ShowHeader="False" Width="649px"
OnRowEditing="gdvFiles_RowEditing" OnRowUpdating="gdvFiles_RowUpdating">
        .....
                      <asp:TemplateField HeaderText="FileType"
ShowHeader="False" Visible="False">
                            <HeaderStyle Width="30px"
HorizontalAlign="Center" VerticalAlign="Middle" />
                            <ItemStyle Width="30px" HorizontalAlign="Center"
VerticalAlign="Middle" />
                            <ItemTemplate>                                  

                                <asp:Label ID="lblFileType" runat="server"
Text='<%# Eval("FileType") %>'></asp:Label>
                            </ItemTemplate>    
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddlType"
runat="server" DataSource='<%# LoadFileTypes() %>'
DataTextField="FileTypeCode" DataValueField="FileTypeCode"
OnSelectedIndexChanged="ddlType_SelectedIndexChanged"></asp:DropDownList>
                            </EditItemTemplate>
                        </asp:TemplateField>
        .....
        </asp:GridView>


I need to process the values of this Gridview cells inside in this method:
   private void UpdateRelatedFiles(Int32 ideaId, Int32 userId, Int32 langId)
   {
    ....
    RetrieveTempData();
        LoadFileGrid(true);

    foreach (GridViewRow gr in gdvFiles.Rows)
        {
            id = ((HtmlInputCheckBox)gr.Cells[0].Controls[1]).Value;
            fileId = Convert.ToInt32(id);
            fname = gr.Cells[1].Text;
            ftype = gr.Cells[2].Text;<<<--- STRING VALUE IS EMPTY
            fsize = gr.Cells[3].Text;
            fpath = gr.Cells[4].Text;
        ....
    }
    ....
   }

    private void RetrieveTempData()
    {      
    ....
        dtFiles = GetTempData("Files");
        ....
    }

    private DataTable GetTempData(string choice)
    {
        try
        {
            if (...)
        ....
            else if (choice == "Files")
                return (DataTable) Session["dtFiles"];       
        }
        catch
        {
            return null;
        }
        return null;
    }

    private void LoadFileGrid(bool colVisible)
    {
        if (dtFiles != null)
        {
            DataTable dtLocal = GetTempData("Files");
            if (dtLocal != null)
                gridview1.DataSource = dtLocal;
            else
                gridview1.DataSource = dtFiles;           
            ChangeGridviewColumnVisibility(colVisible, gridview1, "Files",
true);
            gridview1.DataBind();
        }
    }

    private void ChangeGridviewColumnVisibility(bool visible, GridView
gdvLocal, string section, bool isUpdate)
    {

        for (int x = 0; x < gdvLocal.Columns.Count; x++)
        {
            if (visible)
            {
                if (gdvLocal.Columns[x].HeaderText == "DocumentID" &&
isUpdate)
                    gdvLocal.Columns[x].Visible = visible;
                else
                    gdvLocal.Columns[x].Visible = visible;
            }
            else if (!visible)
            {
                if (x > 1 && x < gdvLocal.Columns.Count - 1)
                    gdvLocal.Columns[x].Visible = visible;
            }
        }
    }

[/code]
--
MCP Year 2005, Philippines

Author
11 Oct 2006 11:44 AM
Antoine Griffard
Hi Den,

To retrieve the value, use the same code that for the checkBox.
You have to find the first control of the cell which is your dropdownlist :

ftype = ((DropDownList)gr.Cells[2].Controls[1]).SelectedValue;

Show quoteHide quote
> Hi everybody,
>
> I created a Gridview with a TemplateField and there is Label
> control in ItemTemplate and a DropdownList control in
> EditItemTemplate, I was to displayed them ok when I click the Edit
> Command button. But when I about to get the value of this template
> field I get an empty string value. I check the following the local
> dtFiles has data and the cells are visible at stage right before
> foreach GridViewRow gr in gdvFiles.Rows) loop. Can anyone tell what is
> missing or I went wrong? Thanks
>
> [code]
> <asp:GridView ID="gdvFiles" runat="server"
> AutoGenerateColumns="False" ShowHeader="False" Width="649px"
> OnRowEditing="gdvFiles_RowEditing"
> OnRowUpdating="gdvFiles_RowUpdating">
> .....
> <asp:TemplateField HeaderText="FileType"
> ShowHeader="False" Visible="False">
> <HeaderStyle Width="30px"
> HorizontalAlign="Center" VerticalAlign="Middle" />
> <ItemStyle Width="30px" HorizontalAlign="Center"
> VerticalAlign="Middle" />
> <ItemTemplate>
> <asp:Label ID="lblFileType"
> runat="server"
> Text='<%# Eval("FileType") %>'></asp:Label>
> </ItemTemplate>
> <EditItemTemplate>
> <asp:DropDownList ID="ddlType"
> runat="server" DataSource='<%# LoadFileTypes() %>'
> DataTextField="FileTypeCode" DataValueField="FileTypeCode"
> OnSelectedIndexChanged="ddlType_SelectedIndexChanged"></asp:DropDownLi
> st>
> </EditItemTemplate>
> </asp:TemplateField>
> .....
> </asp:GridView>
> I need to process the values of this Gridview cells inside in this
> method:
> private void UpdateRelatedFiles(Int32 ideaId, Int32 userId, Int32
> langId)
> {
> ....
> RetrieveTempData();
> LoadFileGrid(true);
> foreach (GridViewRow gr in gdvFiles.Rows)
> {
> id = ((HtmlInputCheckBox)gr.Cells[0].Controls[1]).Value;
> fileId = Convert.ToInt32(id);
> fname = gr.Cells[1].Text;
> ftype = gr.Cells[2].Text;<<<--- STRING VALUE IS EMPTY
> fsize = gr.Cells[3].Text;
> fpath = gr.Cells[4].Text;
> ....
> }
> ....
> }
> private void RetrieveTempData()
> {
> ....
> dtFiles = GetTempData("Files");
> ....
> }
> private DataTable GetTempData(string choice)
> {
> try
> {
> if (...)
> ....
> else if (choice == "Files")
> return (DataTable) Session["dtFiles"];
> }
> catch
> {
> return null;
> }
> return null;
> }
> private void LoadFileGrid(bool colVisible)
> {
> if (dtFiles != null)
> {
> DataTable dtLocal = GetTempData("Files");
> if (dtLocal != null)
> gridview1.DataSource = dtLocal;
> else
> gridview1.DataSource = dtFiles;
> ChangeGridviewColumnVisibility(colVisible, gridview1,
> "Files",
> true);
> gridview1.DataBind();
> }
> }
> private void ChangeGridviewColumnVisibility(bool visible, GridView
> gdvLocal, string section, bool isUpdate)
> {
> for (int x = 0; x < gdvLocal.Columns.Count; x++)
> {
> if (visible)
> {
> if (gdvLocal.Columns[x].HeaderText == "DocumentID" &&
> isUpdate)
> gdvLocal.Columns[x].Visible = visible;
> else
> gdvLocal.Columns[x].Visible = visible;
> }
> else if (!visible)
> {
> if (x > 1 && x < gdvLocal.Columns.Count - 1)
> gdvLocal.Columns[x].Visible = visible;
> }
> }
> }
> [/code]
>