0

I have already a populated GridView working perfect, But now I need to set some parameters. I'm populating the gridview in HTML page. Here's my FULL GridView HTML code Hope you guys don't consider it as an spam, because its a huge code.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="font-family: Verdana, Arial, Sans-Serif" 
            DataKeyNames="id"
            CssClass="gridview" DataSourceID="MyDataSource"
            AllowSorting ="True" AllowPaging="True" BackColor="#CCCCCC" 
            BorderStyle="Inset" BorderWidth="2px" BorderColor="GrayText"
            CellPadding="1"
            CellSpacing="5"
            HeaderStyle-HorizontalAlign="Center"
            OnRowDataBound="GridView1_RowDataBound" 
            ForeColor = "Black" RowStyle-CssClass="gridview" 
            onrowcommand="GridView1_RowCommand">            
            <AlternatingRowStyle BackColor="#CCCCCC" />
                <columns>

                    <asp:BoundField HeaderText="ID" DataField="id"  />
                    <asp:BoundField HeaderText="PRIORIDADE" DataField="prioridade" SortExpression="prioridade" ItemStyle-HorizontalAlign="Center" />
                    <asp:BoundField  HeaderText="SITUAÇÃO"  DataField="situacao" SortExpression="situacao" ItemStyle-HorizontalAlign="Center" >
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="RESPONSAVEL" DataField="responsavel" SortExpression="responsavel" HeaderStyle-Width="65px" ItemStyle-HorizontalAlign="Center">
                    <HeaderStyle Width="65px" />
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="DATA DE CADASTRO" DataField="dt_cadastro" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px"
                    SortExpression="dt_cadastro" ItemStyle-HorizontalAlign="Center" >
                    <HeaderStyle Width="60px" />
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="PREVISÃO DE TÉRMINO" DataField="previsao_termino" DataFormatString="{0:dd/MM/yyyy}" HeaderStyle-Width="60px"
                    SortExpression="previsao_termino" ItemStyle-HorizontalAlign="Center">
                    <HeaderStyle Width="60px" />
                    <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="PROJETO" DataField="projeto"  ItemStyle-HorizontalAlign="Center"></asp:BoundField>
                    <asp:BoundField HeaderText="FUNCIONALIDADE" DataField="funcionalidade" ItemStyle-HorizontalAlign="Center" />
                    <asp:BoundField HeaderText="CLUBE" DataField="clube" SortExpression="clube" ItemStyle-HorizontalAlign="Center" />

                    <asp:TemplateField HeaderStyle-Width="70px" HeaderText="VISUALIZAR" >
                        <ItemTemplate>
                            <asp:Button ID="Btn_Visualizar" runat="server" Text="VISUALIZAR" CssClass="Btn_Grid"  Font-Size="7pt" Font-Names="Verdana, Arial"
                            CommandName="visualizar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />                            
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderStyle-Width="66px" HeaderText="ALTERAR">
                        <ItemTemplate>
                            <asp:Button ID="Btn_Alterar" runat="server" Text="ALTERAR" CssClass="Btn_Grid" Font-Size="7pt" Font-Names="Verdana, Arial"
                            CommandName="editar" CommandArgument="<%# ((GridViewRow)Container).RowIndex %>" />
                        </ItemTemplate>
                    </asp:TemplateField>


                </columns>
            <EditRowStyle ForeColor="Black" CssClass="GridViewEditRow" />
            <FooterStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" BorderColor="White" BorderStyle="Solid" BorderWidth="1px" />
            <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
            <RowStyle BackColor="White" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />      
            </asp:GridView>           

            <asp:SqlDataSource ID="MyDataSource" runat="server"
               ConnectionString="server=localhost;User Id=xx;password=xxxx;database=xxxx"
               ProviderName ="MySql.Data.MySqlClient"

               SelectCommand="SELECT ch.id, sit.descricao as situacao, resp.responsavel, ch.dt_cadastro, ch.previsao_termino, func.descricao as funcionalidade, 
                              proj.descricao as projeto ,pr.id as prid, pr.prioridade, clb.clube
                              FROM chamados AS ch  
                              INNER JOIN prioridades as pr  ON ch.prioridade = pr.id
                              INNER JOIN clubes as clb ON ch.clube = clb.id 
                              INNER JOIN responsaveis as resp ON ch.responsavel = resp.id
                              INNER JOIN situacoes as sit ON ch.situacao = sit.id 
                              INNER JOIN projetos as proj ON ch.projeto = proj.id
                              INNER JOIN funcionalidades as func ON ch.funcionalidade = func.id WHERE situacao != 3"

               UpdateCommand="Update chamados SET status = @status , responsavel = @responsavel, 
               previsao_termino = @previsao_termino, titulo = @titulo WHERE id = @id"

               DeleteCommand="Delete FROM chamados WHERE id = @id">
               <DeleteParameters>
                            <asp:Parameter Name="id" Type="Int32" />

               </DeleteParameters>

               <UpdateParameters>
                            <asp:Parameter Name="status" Type="String" />
                            <asp:Parameter Name="responsavel" Type="String" />
                            <asp:Parameter Name="previsao_termino" Type="DateTime" />
                            <asp:Parameter Name="titulo" Type="String" />
                            <asp:Parameter Name="id" Type="Int32" />
               </UpdateParameters>
            </asp:SqlDataSource>

How may I do all of this in Aspx.cs file, do I create a DataSet and set as DataSource to the GridView , and How I set the values to the columns ? Thanks ! Obs: I Also have two butons on the cells. [Comment] - Other solution would be pass a parameterin the code below, is it possible ? I need to pass the value of a Session

Ghaleon
  • 1,186
  • 5
  • 28
  • 55

3 Answers3

1

I finally found out the answer from here!

My aspx code: Here is my Select command with my Parameter @club

 SelectCommand="SELECT ch.id, sit.descricao as situacao, resp.responsavel, ch.dt_cadastro, ch.previsao_termino, func.descricao as funcionalidade, 
                              proj.descricao as projeto ,pr.id as prid, pr.prioridade, clb.clube
                              FROM chamados AS ch  
                              INNER JOIN prioridades as pr  ON ch.prioridade = pr.id
                              INNER JOIN clubes as clb ON ch.clube = clb.id 
                              INNER JOIN responsaveis as resp ON ch.responsavel = resp.id
                              INNER JOIN situacoes as sit ON ch.situacao = sit.id 
                              INNER JOIN projetos as proj ON ch.projeto = proj.id
                              INNER JOIN funcionalidades as func ON ch.funcionalidade = func.id WHERE situacao != 3 AND ch.clube = @club"

<SelectParameters>
                    <asp:Parameter Name="club" Type="String" />
               </SelectParameters>

Then here is my aspx.cscode: Here I set set the value of the parameter, as it is in aspx.cs you can do anything you want to set the value...

MyDataSource.SelectParameters["club"] = new Parameter()
            {
                Name = "club",
                DefaultValue = "2" };

Worked just perfect xD hope it can helpe someone else... Bye Guys!

Community
  • 1
  • 1
Ghaleon
  • 1,186
  • 5
  • 28
  • 55
0

You can do all of these tasks in Code behind page as well. Please refer following link for further reference.

http://www.codeproject.com/Articles/24085/Insert-Update-Delete-with-Gridview-Simple-Way

Kiran1016
  • 914
  • 4
  • 14
  • I'll read it now... But tell me. I have two buttons and these buttons have Functions() May I do it as well ? (Using the method that is in the link you just sent me) Thanks – Ghaleon Jan 21 '13 at 17:15
  • what are those functions ,what you expect from those two buttons. – Kiran1016 Jan 21 '13 at 17:20
  • They Both open a new page with details of the row... One is just `View` and other is `Edit`. Open a new windos, one I just see the details and the other, I can Edit de `status` – Ghaleon Jan 21 '13 at 17:29
  • you can perform both functionalities in the code behind page.you can call the same pages from rowcommand event as per the command name you given for the buttons.Ex:item.commanname="visualizar" then do the same thing what you are doing now,same for edit – Kiran1016 Jan 21 '13 at 17:35
  • That's how I'm doing now... I just need to know How to pass a `parameter` value to the gridview the way it is now, I guess that would be easier... – Ghaleon Jan 21 '13 at 17:53
  • Hmm... Could you give me a little example ? All I need is to `add` this line in the `SqlDataSource`: Where club = (SELECT name FROM users WHERE login = Session["login"].ToString()); I could do it easy in aspx.cs, BUt i dont know how to do it in this situation ;\ – Ghaleon Jan 21 '13 at 18:00
  • please read this link it has clear Steps.http://stackoverflow.com/questions/3319358/changing-sqldatasource-selectcommand-at-runtime-breaks-pagination – Kiran1016 Jan 21 '13 at 18:15
0
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

getGrid();

    }
    public string GetCon()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString.ToString();
    }

    public void getGrid()
    {

        GridView grd = new GridView();        
        Page.Controls.Add(grd);
        form1.Controls.Add(grd);

        grd.ControlStyle.Font.Size = 9;


        SqlConnection conn = new SqlConnection(GetCon());
        conn1.Open();
        SqlCommand cmd4grid = new SqlCommand("Select * from tblCustomers", conn);
        SqlDataAdapter dad = new SqlDataAdapter(cmd4grid);
        DataTable dt = new DataTable();
        dad.Fill(dt);
        grd.DataSource = dt;
        grd.DataBind();
    }
}

**strong text**
Phyco
  • 1