GridView Getting Started...

Last post 06-21-2009 1:05 AM by moodi_z. 4 replies.

Sort Posts:

  • GridView Getting Started...

    06-17-2009, 10:21 AM
    • Member
      12 point Member
    • moodi_z
    • Member since 03-09-2009, 8:15 AM
    • Posts 84

     Hi, I'm new with asp.net and I need to use gridview but I'm not succeding with the rowupdating event.
    My Code:

    protected void POGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            SqlConnection conn = null;
            SqlCommand sqlComm = null;
            SqlDataReader rdr = null;
    
            /*DataTable dt = (DataTable)????;
            GridViewRow row = POGridView.Rows[e.RowIndex];
            dt.Rows[row.DataItemIndex]["Status"] = ((DropDownList)(row.Cells[5].Controls[0])).SelectedItem;
            dt.Rows[row.DataItemIndex]["Priority"] = ((TextBox)(row.Cells[6].Controls[0])).Text;
            POGridView.EditIndex = -1;
            BindGridView(ddlRoutes, new EventArgs());*/
    
            GridView gv = (GridView)sender;
            for (int i = 0; i < gv.Columns.Count; i++)
            {
                DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
                gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, false);
    
            }
    
            try
            {
                conn = new SqlConnection(Globals.conString);
                conn.Open();
    
                sqlComm = new SqlCommand("Update tbl_PO SET Status='" + e.NewValues[5].ToString () +
                                            "', Priority=" + e.NewValues[6] +
                                            " Where POID = '111'", conn);
                rdr = sqlComm.ExecuteReader();
            }
    
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
    
            //e.Cancel = true;
            //POGridView.EditIndex = -1;
            //BindGridView(ddlRoutes, new EventArgs()); 
    
        }

    Although I assigned false in :
    gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, false); to avoid read only fields I'm not getting the values of non-read only ones that I had edited!!!

    How does it work!

    My Grid looks like that:

    <asp:GridView ID="POGridView" 
    Width="80%"
    AllowPaging="false"
    PageSize="10"
    AllowSorting="false"
    DataKeyNames="POID"
    HeaderStyle-BackColor="#00A3E4"
    AlternatingRowStyle-BackColor="AliceBlue"
    AutoGenerateEditButton="true"
    EditRowStyle-BackColor="#D3EDF3"
    AutoGenerateColumns="false"
    AutoGenerateDeleteButton="false"
    AutoGenerateSelectButton="false"
    OnRowEditing="POGridView_RowEditing"
    OnRowCancelingEdit ="POGridView_RowCancelingEdit"
    OnRowUpdating="POGridView_RowUpdating"
    OnPageIndexChanging="POGridView_PageIndexChanging"
    OnSelectedIndexChanging="POGridView_SelectedIndexChanging"
    Visible="true"
    runat="server">

    <Columns>
    <asp:BoundField HeaderText="POUID" Visible ="false" DataField="POUID" />
    <asp:BoundField HeaderText="POID" DataField="POID" ReadOnly="true" />
    <asp:BoundField HeaderText="CatalogID" ReadOnly="true" DataField="CatalogID" />
    <asp:BoundField HeaderText="CatalogDesc" ReadOnly="true" DataField="Description" />
    <asp:BoundField HeaderText="RequestedQty" ReadOnly="true" DataField="RequestedQty" />
    <asp:BoundField HeaderText="Status" ReadOnly="true" DataField="Status" Visible ="true" />
    <asp:TemplateField HeaderText="Status" Visible ="false">
    <ItemTemplate>
    <asp:Label ID="lblStatus" runat="server"
    Text='<%# Eval("Status") %>' />
    </ItemTemplate>
    <EditItemTemplate>
    <asp:DropDownList ID="ddlStatus" runat="server"
    Width="100px"
    DataSourceID="dsStatus"
    DataTextField="StatusID"
    DataValueField="StatusID">
    </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:BoundField HeaderText="Priority" DataField="Priority" />
    </Columns>
    </asp:GridView>

    Note: I faced a problem with saving the data of the status. So I made two columns, one to keep the value and one to change it later, I did that cause I'm filling the status list with values that I get from a stored procedure that get the old value as a parameter:

     <asp:BoundField HeaderText="Status" ReadOnly="true" DataField="Status" Visible ="true" /> 
    <asp:TemplateField HeaderText="Status" Visible ="false">

    isn't there a better way to solve it?!

    Best Regards.

     
  • Re: GridView Getting Started...

    06-17-2009, 1:41 PM
    Answer

     convert all those bound fields to template fields.

    Then when you observer the source of the gridview, each template field has 2 sections, one is itemtemplate and another one is edititemtemplate 

    item template consists of labels and edit template consists of textboxes. and for read-only fields, edititemtemplate has only labels.

    then in rowupdating event, you can get the values by findcontrol() method and u can get those values.

    suppose, for a column, in its edittemplate, there is a textbox like below

      <EditItemTemplate>

        <asp:TextBox ID="txtValue" runat="server" Text='<%# Bind("value") %>'></asp:TextBox>


     </EditItemTemplate>

     

    then in rowupdating event you can find that by 

    TextBox txtValue  = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtValue");

    string value = txtValue.Text;

    similarly u canfind other column values also.

    Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
  • Re: GridView Getting Started...

    06-18-2009, 1:02 AM
    • Member
      12 point Member
    • moodi_z
    • Member since 03-09-2009, 8:15 AM
    • Posts 84

     Thank you very much, it works.

    I have another question if u don't mined... I need to handle authorizations, I mean to enable the edit button for some users and to disable it to others according to there authorizations.

    How can I control the visibility of the gridview edit button?

  • Re: GridView Getting Started...

    06-18-2009, 1:29 AM
    Answer
    • Member
      246 point Member
    • meetmanthan
    • Member since 06-15-2009, 7:45 AM
    • Pune
    • Posts 78

    Use follwoing code for handeling authorizations.

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
             if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
              {
                    if(currentUser.CanEdit == False)
                        e.Row.Cells[2].Visible = false; // i am assuming that index of edit button is 2. count your colomn in gridview and change index 2 to index of edit button.
              }
     }

    Which will not display edit button colomn for un authorize user to edit.

    Regards,
    Manthan Upadhyay.

    Make it "answer" if it helps you to solve ur query.
  • Re: GridView Getting Started...

    06-21-2009, 1:05 AM
    • Member
      12 point Member
    • moodi_z
    • Member since 03-09-2009, 8:15 AM
    • Posts 84

     Great , thank you.

Page 1 of 1 (5 items)