gridview onrowcommand

satya - Monday, May 28, 2007 10:00:11 PM

GridViewCommandEventArgs

GridViewCommandEventArgs

satya - Monday, May 28, 2007 10:06:43 PM

gridview.datakeys

gridview.datakeys

satya - Monday, May 28, 2007 10:09:53 PM

what are datakeys

When the DataKeyNames property is set, the GridView control automatically creates a DataKey object for each row in the control. The DataKey object contains the values of the field or fields specified in the DataKeyNames property. The DataKey objects are then added to the control's DataKeys collection. Use the DataKeys property to retrieve the DataKey object for a specific data row in the GridView control.

satya - Monday, May 28, 2007 10:28:43 PM

understand datakey

understand datakey

satya - Monday, May 28, 2007 10:38:59 PM

defining a grid for command


<asp:gridview id="CustomersGridView" 
              datasourceid="CustomersSource"
              allowpaging="true" 
              autogeneratecolumns="false"
              onrowcommand="CustomersGridView_RowCommand"
              onrowcreated="CustomersGridView_RowCreated"  
              runat="server">

satya - Monday, May 28, 2007 10:39:50 PM

a link button


<asp:buttonfield buttontype="Link" 
                  commandname="Add" 
                  text="Add"/>

satya - Monday, May 28, 2007 10:40:48 PM

samplecode


void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Add")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);
            
      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection.
      GridViewRow row = CustomersGridView.Rows[index];
            
      // Create a new ListItem object for the customer in the row.     
      ListItem item = new ListItem();
      item.Text = Server.HtmlDecode(row.Cells[2].Text);
            
      // If the customer is not already in the ListBox, add the ListItem 
      // object to the Items collection of the ListBox control. 
      if (!CustomersListBox.Items.Contains(item))
      {
        CustomersListBox.Items.Add(item);
      }           
    }
  }

  void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {
    
    // The GridViewCommandEventArgs class does not contain a 
    // property that indicates which row's command button was
    // clicked. To identify which row's button was clicked, use 
    // the button's CommandArgument property by setting it to the 
    // row's index.
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Retrieve the LinkButton control from the first column.
      LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
          
      // Set the LinkButton's CommandArgument property with the
      // row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString();
    }

  }

satya - Monday, May 28, 2007 10:48:03 PM

The story of a linkbutton

The story of a linkbutton

satya - Monday, May 28, 2007 10:51:58 PM

Do I have to set the index of the row in the rowcreated event?

Do I have to set the index of the row in the rowcreated event?

satya - Tuesday, May 29, 2007 3:09:51 PM

allowed columns in a gridview


BoundField
ButtonField
CheckboxField
CommandField
HyperLinkField
ImageField
TemplateField

satya - Tuesday, May 29, 2007 3:10:56 PM

ButtonField types


Button
Image
Link

satya - Tuesday, May 29, 2007 3:11:18 PM

what is the difference between buttonfield and commandfield?

what is the difference between buttonfield and commandfield?

satya - Tuesday, May 29, 2007 3:28:47 PM

See here for understanding the hierarchy based on datacontrolfield

See here for understanding the hierarchy based on datacontrolfield

satya - Tuesday, May 29, 2007 3:34:28 PM

hierarchy of datacontrols fields


BoundField
    AutoGeneratedField
    CheckBoxField
ButtonFieldBase
    ButtonField
    CommandField
HyperlinkField
ImageField
TemplateField

satya - Tuesday, May 29, 2007 3:40:31 PM

More on commandfield

The CommandField class is a special field used by data-bound controls (such as GridView and DetailsView) to display command buttons that perform delete, edit, insert, or select operations. The command buttons to perform these operations can be shown or hidden using the properties shown in the following table.

satya - Tuesday, May 29, 2007 3:48:52 PM

ButtonField and CommandField

Looks like asp.net has coded the idea of CRUD separately into this commandfield. With that in mind for general purpose commands one may want to use the buttonfield with a certain type. Essentially a column that shows one or all of CRUD operations is modelled using the commandfield.

satya - Tuesday, May 29, 2007 3:49:16 PM

Bringing it all together: first attempt


<asp:GridView runat="server" ID="queryGridControl" 
     OnRowCommand="OnRowCommand_ShowQuery"
      DataKeyNames = "query_id"
    AutoGenerateColumns="false" Width="100%">
<Columns>
    <asp:ButtonField  
        ButtonType="Link" 
        DataTextField="query_name" 
        DataTextFormatString="{0}" 
        text="Saved Queries" 
        CommandName="showQueryCommand" />
</Columns>
</asp:GridView>

satya - Tuesday, May 29, 2007 3:50:50 PM

Across all the fields the way you bound variable text to a column display seem to be different

Across all the fields the way you bound variable text to a column display seem to be different

satya - Tuesday, May 29, 2007 3:51:53 PM

Exercise

For each field document the variations in which text is associated with what is shown in the column. Also see if there are any references to this in the literature.

satya - Tuesday, May 29, 2007 4:49:10 PM

One more Example of the onrowcommand


protected void OnRowCommand_ShowQuery(Object sender, 
                                GridViewCommandEventArgs e)
    {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "showQueryCommand")
        {
            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Retrieve the row that contains the button clicked 
            // by the user from the Rows collection.
            DataKey dk = this.queryGridControl.DataKeys[index];
            int query_id = Convert.ToInt32(dk.Value);
        }
    }

satya - Tuesday, May 29, 2007 4:50:30 PM

Observation

Looks like the e.commandargument seem to be automatically set to the the row index. This means one doesnt need to use the onrowcreate method to set this. Validate this from literature if possible.

satya - Saturday, June 09, 2007 9:39:54 AM

button type reference

button type reference

satya - Saturday, June 09, 2007 9:42:25 AM

ButtonField properties

ButtonField properties

satya - Saturday, June 09, 2007 9:43:41 AM

Example of a button field using an image


<asp:buttonfield buttontype="Image" 
      commandname="Select"
      headertext="Select Author"
      ImageUrl="~\images\ButtonImage.jpg"/>

satya - Saturday, June 09, 2007 10:20:56 AM

delete icons

Search for: delete icons

satya - Saturday, June 09, 2007 10:28:32 AM

A sample delete icon

A sample delete icon

satya - Saturday, June 09, 2007 10:45:21 AM

are there any free icon websites?

Search for: are there any free icon websites?

satya - Saturday, June 09, 2007 11:37:16 AM

Using an image button link


<asp:GridView runat="server" ID="queryGridControl" 
     OnRowCommand="OnRowCommand_ShowQuery"
      DataKeyNames = "query_id"
    AutoGenerateColumns="false" Width="100%">
<Columns>
    <asp:ButtonField  
        ButtonType="Link" 
        DataTextField="query_name" 
        DataTextFormatString="{0}" 
        HeaderText="Query"
         HeaderStyle-HorizontalAlign="Left"
        CommandName="showQueryCommand" />
    <asp:ButtonField  
        ButtonType="Image" 
         ImageUrl="~/images/delete-icon.png"
         HeaderText="Do"
         ItemStyle-HorizontalAlign="Center"
          ItemStyle-Width="10%"
        CommandName="deleteRowCommand" />
</Columns>
</asp:GridView>

satya - Saturday, June 09, 2007 11:48:11 AM

Responding to the image link


protected void OnRowCommand_ShowQuery(Object sender, 
                                GridViewCommandEventArgs e)
    {
        // Convert the row index stored in the CommandArgument
        // property to an Integer.
        int index = Convert.ToInt32(e.CommandArgument);

        // Retrieve the row that contains the button clicked 
        // by the user from the Rows collection.
        DataKey dk = this.queryGridControl.DataKeys[index];
        int query_id = Convert.ToInt32(dk.Value);

        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if (e.CommandName == "showQueryCommand")
        {
            this.showQueryCommand(query_id);
        }
        else if (e.CommandName == "deleteRowCommand")
        {
            this.deleteRowCommand(query_id);
        }
    }