Display Document type in Gridview

Display Document  type in Gridview


Firstly  i want to explain Document type.
In .net we normally use file up-loader for uploading file in system.it can easily upload file of any type like (word,excel,pdf.etc.....).so if we want view uploaded file content we use gridview.

so its very easy to identify its doc type in gridview.

For this we need to create Gridview .and inside gridview i am using a image control for display its type in thr form of picture.



.aspx



<asp:GridView ID="gr_doc" runat="server"  ItemStyle-VerticalAlign="Top" 
        ItemStyle-HorizontalAlign="Justify" AutoGenerateColumns="false"
                         BorderWidth="0" CellPadding="10" CellSpacing="1" 
        GridLines="None" Width="100%" 
                         AlternatingRowStyle-BackColor="#DDDDDD" 
        onrowdatabound="gr_doc_RowDataBound">
                            <AlternatingRowStyle CssClass="AlternatingRowStyle" />

                    <HeaderStyle CssClass="ColumnHeaderStyle" />
                          <Columns>
                          <asp:TemplateField HeaderText="S.no" ItemStyle-CssClass="colPaddingRow labelnm" HeaderStyle-Width="15%" HeaderStyle-Font-Size="11px" HeaderStyle-CssClass="colPadding labelnm"
                                HeaderStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Justify">
               <ItemTemplate>
                   <%#Container.DataItemIndex+1 %>
                  <asp:HiddenField ID="hdnAccntType" runat="Server" Value='<%#Eval("doc_path") %>' />
                  
                  
                   </ItemTemplate>
                   
              
              
               </asp:TemplateField>
                <asp:BoundField  HeaderText="Dms Id" DataField="dms_id" ItemStyle-CssClass="colPaddingRow" HeaderStyle-Width="15%" HeaderStyle-Font-Size="10px" HeaderStyle-CssClass="colPadding labelnm"
                                HeaderStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Justify" />
              <asp:BoundField  HeaderText="Document Name" DataField="doc_name" ItemStyle-CssClass="colPaddingRow" HeaderStyle-Width="15%" HeaderStyle-Font-Size="10px" HeaderStyle-CssClass="colPadding labelnm"
                                HeaderStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Justify"/>


                                <asp:BoundField  HeaderText="Category" DataField="dms_cat" ItemStyle-CssClass="colPaddingRow" HeaderStyle-Width="15%" HeaderStyle-Font-Size="10px" HeaderStyle-CssClass="colPadding labelnm"
                                HeaderStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Justify"/>

              <asp:TemplateField  HeaderText="View Detail" ItemStyle-CssClass="colPaddingRow" HeaderStyle-Width="15%" HeaderStyle-Font-Size="10px" HeaderStyle-CssClass="colPadding labelnm"
                                HeaderStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Justify">
              <ItemTemplate>
              <a href='<%#Eval("doc_path") %>' target="_blank"><asp:Image ID="Image1" runat="server"></asp:Image></a>
              </ItemTemplate>

              
              </asp:TemplateField>

               </Columns>
                         </asp:GridView>




In this gridview i am using its RowDataBound Event for this..


.Cs


For populating gridview...

 public void bind_grid()
    {
        string qu_doc = "select * from dms where is_active=1";
        SqlDataAdapter ad = new SqlDataAdapter(qu_doc, con);
        DataTable dt = new DataTable();
        ad.Fill(dt);
        gr_doc.DataSource = dt;
        gr_doc.DataBind();
    }



And here its RowDataBound...


protected void gr_doc_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            //Calculate Total



            HiddenField hid = e.Row.FindControl("hdnAccntType") as HiddenField;
            string ss = Convert.ToString( hid.Value);

            Image im = e.Row.FindControl("Image1") as Image;
            
            if (ss.Contains(".jpg") ||ss.Contains(".JPG")|| ss.Contains(".jpeg") ||ss.Contains(".JPEG")|| ss.Contains(".gif") ||ss.Contains(".GIF")|| ss.Contains(".png")||ss.Contains(".PNG"))
            {
            
            //http://localhost:1046/Metro UI_21/intranet/Employee_KRA/images/jpegicon.jpg

                im.Attributes.Add("Src", "images/jpgicon3.png");
            
            }
            else if (ss.Contains(".pdf") || ss.Contains(".PDF"))
            {
                im.Attributes.Add("Src", "images/pdficon3.png");
            }
            else if (ss.Contains(".doc") || ss.Contains(".DOC") || ss.Contains(".DOCX") || ss.Contains(".docx"))
            {
                im.Attributes.Add("Src", "images/wordicon.png");
            }
            else if (ss.Contains(".XLS") || ss.Contains(".xls") || ss.Contains(".XLXS") || ss.Contains(".xlxs"))
            {
                im.Attributes.Add("Src", "images/excel2.gif");
            }
        }
    }



The output like this...



By this you can easily Identify its File type Either it is word,pdf,jpg,execletc...
You can also add more type just do some changes in rowDataBound Portion.










Comments

Post a Comment

Popular posts from this blog

Create and save QR code in asp.net with C#

Change text of RadGrid Header Dynamically

Telerik Radwindow Open on Button Click