GridView Question ?!http://forums.asp.net/t/1091830.aspx/1?GridView+Question+Tue, 27 Mar 2007 15:02:55 -040010918301638788http://forums.asp.net/p/1091830/1638788.aspx/1?GridView+Question+GridView Question ?! <p>Hello,</p> <p>I am getting a little bit frustrated now.</p> <p>I have a drop down menu holding different types of an item. when i choose that item from drop down menu, those items under that type show up in a Grid View. up to here everything is perfect.</p> <p>then I have a detailview that let me add new items to that list (grid View) and eveything is perfect even up to here.</p> <p>the only problem is when I add the new item the GridView doesnt show the newly added item, I have to change the drop down to something else and then select another one again to see the newly added one. </p> <p>This is really annoying, I want to see that new item straight away in my Grid View. What is wrong ? do I need to do something to refresh my gridview ?</p> <p>&nbsp;<br> Thank you.<br> <br> <br> <br> <br> <br> &nbsp;</p> <p>&nbsp;<br> <br> &nbsp;</p> 2007-03-27T10:28:15-04:001638986http://forums.asp.net/p/1091830/1638986.aspx/1?Re+GridView+Question+Re: GridView Question ?! <p>you possibly need to re-bind your gridview when a new item is inserted. <b>GridView1.DataBind(); </b></p> <p>&nbsp;</p> 2007-03-27T13:01:30-04:001639072http://forums.asp.net/p/1091830/1639072.aspx/1?Re+GridView+Question+Re: GridView Question ?! <p>Thanks but this didnt help also.</p> <p>&nbsp;By the way, I am using two update panels one for Grid View and one for Detail View. Update mode for Grid View is Always and for Detail View is conditional !</p> <p>&nbsp;<br> The drop down menu is at the top out side of any Update Panel.</p> <p>&nbsp;<br> Is there anything that might cause the problem ?</p> <p>&nbsp;</p> <p>On the same page I have another similare situation where I add the new element using Detail View and I see the changes in GridView but there is no drop down menu there. </p> <p>&nbsp;<br> So I am assuming that the drop down menu is doing something wrong here or is it ?</p> <p>&nbsp;</p> 2007-03-27T13:39:16-04:001639229http://forums.asp.net/p/1091830/1639229.aspx/1?Re+GridView+Question+Re: GridView Question ?! <p>try putting the dropdownlist inside of the first update panel (where the gridview is) with the default settings. make sure that your dropdownlist postback property is set to true.</p> <p>Below is a snippet from a project im working on that uses the gridview and details view in pretty much the same fashion you are describing. It doesnt allow for you to insert data, but you can see how I use it for deleting an item and the gridview updates accordingly. Pretty much uses the same guidelines. (the sample below is a simple resume viewer for a list of job applicants)<br> </p> <p><b>aspx:</b></p> <pre class="prettyprint">&lt;!-- Update Panel --&gt; &lt;asp:UpdatePanel ID=&quot;UpdatePanel1&quot; runat=&quot;server&quot; &gt; &lt;ContentTemplate&gt; &lt;!-- Search template --&gt; &lt;table style=&quot;border:solid 1 white;&quot;&gt; &lt;tr&gt; &lt;td style=&quot;width:170px;&quot; class=&quot;header&quot;&gt;Applicant Name&lt;/td&gt; &lt;td style=&quot;width:170px;&quot; class=&quot;header&quot;&gt;Position&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td style=&quot;width:170px;&quot;&gt; &lt;asp:Panel ID=&quot;pnlSearch&quot; runat=&quot;server&quot; DefaultButton=&quot;lnkbtnSearch&quot;&gt; &lt;asp:TextBox ID=&quot;txtName&quot; runat=&quot;server&quot; /&gt; &lt;asp:ImageButton ID=&quot;lnkbtnSearch&quot; runat=&quot;server&quot; ImageUrl=&quot;~/images/search.gif&quot; OnClick=&quot;FilterExpression&quot; /&gt;&lt;/asp:Panel&gt;&lt;/td&gt; &lt;td style=&quot;width:170px;&quot;&gt; &lt;asp:DropDownList ID=&quot;ddlPosition&quot; runat=&quot;server&quot; AutoPostBack=&quot;true&quot; DataSourceID=&quot;sqldsPositions&quot; DataTextField=&quot;fldPosition&quot; AppendDataBoundItems=&quot;true&quot; Width=&quot;173px&quot; OnSelectedIndexChanged=&quot;FilterExpression&quot; &gt; &lt;asp:ListItem&gt;All&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;!-- Grid View --&gt; &lt;asp:GridView ID=&quot;gvJobApplicants&quot; runat=&quot;server&quot; CssClass=&quot;gridviewstyle&quot; DataSourceID=&quot;sqldsJobApplicants&quot; DataKeyNames=&quot;idJobApp&quot; AutoGenerateColumns=&quot;false&quot; GridLines=&quot;Vertical&quot; AllowPaging=&quot;true&quot; AllowSorting=&quot;true&quot;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&quot;Sort&quot; SortExpression=&quot;fldLastName&quot; &gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&quot;lnkbtnName&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;fldLastName&quot;) &#43; &quot;, &quot; &#43; Eval(&quot;fldFirstName&quot;) %&gt;' CommandName=&quot;Select&quot; /&gt; &lt;/ItemTemplate&gt; &lt;ItemStyle CssClass=&quot;itemstyle&quot; /&gt; &lt;HeaderStyle CssClass=&quot;header&quot; /&gt; &lt;/asp:TemplateField&gt; &lt;asp:BoundField HeaderText=&quot;Sort&quot; DataField=&quot;fldPosition&quot; SortExpression=&quot;fldPosition&quot; &gt; &lt;ItemStyle CssClass=&quot;itemstyle&quot; /&gt; &lt;HeaderStyle CssClass=&quot;header&quot; /&gt; &lt;/asp:BoundField&gt; &lt;/Columns&gt; &lt;EmptyDataTemplate&gt; &lt;div class=&quot;norecords&quot;&gt;&lt;img src=&quot;../images/caution.gif&quot; alt=&quot;&quot; /&gt; No records found.&lt;/div&gt; &lt;/EmptyDataTemplate&gt; &lt;/asp:GridView&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;!-- DataSource Block --&gt; &lt;asp:SqlDataSource ID=&quot;sqldsDetails&quot; runat=&quot;server&quot; SelectCommand=&quot;GetApplicantDetails&quot; SelectCommandType=&quot;StoredProcedure&quot; ConnectionString=&quot;&lt;%$ ConnectionStrings: connString %&gt;&quot;&gt; &lt;SelectParameters&gt; &lt;asp:ControlParameter Name=&quot;idJobApp&quot; ControlID=&quot;gvJobApplicants&quot; PropertyName=&quot;SelectedValue&quot; /&gt; &lt;/SelectParameters&gt; &lt;/asp:SqlDataSource&gt; &lt;!-- Update Panel --&gt; &lt;asp:UpdatePanel ID=&quot;updtepnlClientNews&quot; runat=&quot;server&quot; &gt; &lt;ContentTemplate&gt; &lt;!-- Details View --&gt; &lt;div&gt; &lt;asp:DetailsView ID=&quot;dvJobApplicants&quot; runat=&quot;server&quot; CssClass=&quot;gridviewstyle&quot; AutoGenerateRows=&quot;false&quot; DataSourceID=&quot;sqldsDetails&quot; DataKeyNames=&quot;idJobApp&quot; DefaultMode=&quot;ReadOnly&quot; GridLines=&quot;None&quot; width=&quot;450px&quot; &gt; &lt;HeaderTemplate&gt; &lt;div style=&quot;text-align:center;&quot; class=&quot;header&quot;&gt; &lt;asp:Label ID=&quot;ltlName&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;fldFirstName&quot;) &#43; &quot; &quot; &#43; Eval(&quot;fldLastName&quot;) %&gt;' /&gt; &lt;asp:ImageButton ID=&quot;imgbtnDelete&quot; runat=&quot;server&quot; AlternateText=&quot;Delete&quot; ToolTip=&quot;Delete applicant&quot; ImageUrl=&quot;~/images/delete.gif&quot; OnClientClick='return confirm(&quot;Are you sure you want to delete this applicant?&quot;)' CommandName=&quot;DeleteApplicant&quot;/&gt; &lt;/div&gt; &lt;/HeaderTemplate&gt; &lt;Fields&gt; &lt;asp:BoundField HeaderText=&quot;Address&quot; DataField=&quot;fldAddress1&quot; /&gt; &lt;asp:BoundField DataField=&quot;fldAddress2&quot; /&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&quot;lblAddress3&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;fldCity&quot;) &#43; &quot;, &quot; &#43; Eval(&quot;fldState&quot;) &#43; &quot;&amp;nbsp;&quot; &#43; Eval(&quot;fldZip&quot;) %&gt;' /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:BoundField HeaderText=&quot;Day Phone&quot; DataField=&quot;fldDayphone&quot; /&gt; &lt;asp:BoundField HeaderText=&quot;Evening Phone&quot; DataField=&quot;fldEveningPhone&quot; /&gt; &lt;asp:BoundField HeaderText=&quot;Email&quot; DataField=&quot;fldEmail&quot; /&gt; &lt;asp:BoundField HeaderText=&quot;Cover Letter&quot; DataField=&quot;fldCoverLetter&quot; /&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:HiddenField ID=&quot;hiddenFile&quot; runat=&quot;server&quot; Value='&lt;%# Eval(&quot;fldResume&quot;) %&gt;' /&gt; &lt;asp:HyperLink ID=&quot;hyperResume&quot; runat=&quot;server&quot; Text=&quot;View Resume&quot; NavigateUrl='&lt;%# Eval(&quot;fldResume&quot;, &quot;resume.ashx/{0}&quot;) %&gt;' Visible='&lt;%# Visible(Eval(&quot;fldResume&quot;)) %&gt;' /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Fields&gt; &lt;AlternatingRowStyle BackColor=&quot;#2B4D6F&quot; /&gt; &lt;FieldHeaderStyle Width=&quot;100px&quot; Font-Bold=&quot;true&quot; CssClass=&quot;itemstyle&quot;/&gt; &lt;/asp:DetailsView&gt; &lt;/div&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt;</pre><p>&nbsp;&nbsp;</p><p>&nbsp;</p><p><b>Code Behind:</b></p><p>&nbsp;&nbsp;</p><pre class="prettyprint"> <span class="kwd">protected void</span> dvJobApplicants_ItemCommand(<span class="kwd">object</span> sender, DetailsViewCommandEventArgs e) { <span class="kwd">if</span> (e.CommandName == <span class="st">"DeleteApplicant"</span>) { <span class="kwd">string</span> filePath = Request.PhysicalApplicationPath + ((HyperLink)dvJobApplicants.FindControl(<span class="st">"hyperResume"</span>)).NavigateUrl.Replace(<span class="st">"~/"</span>, <span class="kwd">string</span>.Empty); sqldsDetails.DeleteCommand = <span class="st">"DeleteApplicant"</span>; sqldsDetails.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure; sqldsDetails.DeleteParameters.Add(<span class="st">"idJobApp"</span>, TypeCode.Int32, dvJobApplicants.DataKey.Value.ToString()); sqldsDetails.Delete(); <span class="kwd">if</span> (File.Exists(filePath)) { File.Delete(filePath); } gvJobApplicants.DataBind(); } } <span class="kwd">protected void</span> FilterExpression(<span class="kwd">object</span> sender, EventArgs e) { <span class="kwd">string</span> name = txtName.Text; <span class="kwd">string</span> position = ddlPosition.SelectedItem.Text; <span class="kwd">if</span> (position == <span class="st">"All"</span>) { sqldsJobApplicants.FilterExpression = <span class="st">"(fldFirstName LIKE '%"</span> + name + <span class="st">"%' OR fldLastName LIKE '%"</span> + name + <span class="st">"%')"</span>; } <span class="kwd">else</span> { sqldsJobApplicants.FilterExpression = <span class="st">"(fldFirstName LIKE '%"</span> + name + <span class="st">"%' OR fldLastName LIKE '%"</span> + name + <span class="st">"%') AND (fldPosition='"</span> + position + <span class="st">"')"</span>; } } </pre><pre class="prettyprint">&nbsp;</pre><pre class="prettyprint">// <u>I but this method in my basepage class --&gt;</u><u></u></pre><pre class="prettyprint"><u></u><br> <span class="kwd">protected bool</span> Visible(<span class="kwd">object</span> data)<br> {<br> <span class="kwd">if</span> (String.IsNullOrEmpty(data.ToString()))<br> <span class="kwd">return false</span>;<br> <span class="kwd">else<br> return true</span>;<br> }</pre> <p>&nbsp;</p> <p>Hope this helps.&nbsp;</p> 2007-03-27T15:02:55-04:00