Bastardo
Bastardo

Reputation: 4152

Select Button - CommandField on a GridView in ASP.NET

I have this below code to create a GridView on a webpage.I am trying to disable the SelectButton or make it invisible, or just delete it.I tried

AutoGenerateSelectButton="false"

and

<asp:CommandField SelectText="Seç" Visible="false"
                                                                        ShowSelectButton="True"  />

I even deleted this part

<Columns>
          <asp:CommandField          SelectText="Seç"                                                                            ShowSelectButton="True" />
 </Columns> 

None of them worked, the SelectButton is still there. I tried to change it's SelecText to with

<asp:CommandField SelectText="Aç"                                                                            ShowSelectButton="True" />

And this didn't work as well.I also tried

ShowSelectButton="False" and it didn't change anything.

   <asp:UpdatePanel ID="UpdatePanelEnCokSatilanUrunler" runat="server">
             <ContentTemplate>
                      <asp:Panel ID="PanelEnCokSatilanUrunler" runat="server" 
                      GroupingText="En Çok Satılan Ürünler" 
                      BorderWidth="1" Font-Bold="true">
                           <table class="style1">
                               <tr>
                                  <td>
                                      <asp:GridView ID="GridView_EnCokSatilanUrunler" 
                                      runat="server"
                           OnRowDataBound="GridView_EnCokSatilanUrunler_RowDataBound"
                                       Font-Bold="false"
           OnSelectedIndexChanged="GridView_EnCokSatilanUrunler_SelectedIndexChanged"
                                       AllowSorting="true"
                                     OnSorting="GridView_EnCokSatilanUrunler_Sorting">
                                            <Columns>
                                                 <asp:CommandField SelectText="Seç"
                                                     ShowSelectButton="True"/>  
                                            </Columns>         
                                  </asp:GridView>
                     </td>
                    </tr>
                 </table>
               </asp:Panel>

              </ContentTemplate>

            </asp:UpdatePanel>

This is what I have in aspx.cs, which doesn't do anything with the SelectButton

protected void GridView_EnCokSatilanUrunler_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView_EnCokSatilanUrunler_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                ((LinkButton)e.Row.Cells[1].Controls[0]).Text = "Ürün No";
                ((LinkButton)e.Row.Cells[2].Controls[0]).Text = "Ürün Adı";
                ((LinkButton)e.Row.Cells[3].Controls[0]).Text = "Satış Miktarı";
                ((LinkButton)e.Row.Cells[4].Controls[0]).Text = "Ürün Durum";
                ((LinkButton)e.Row.Cells[5].Controls[0]).Text = "Ürün Tipi";
                ((LinkButton)e.Row.Cells[6].Controls[0]).Text = "Marka";
                ((LinkButton)e.Row.Cells[7].Controls[0]).Text = "Model";                
            }
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {

            }
        }

        protected void GridView_EnCokSatilanUrunler_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (EnCokSatilanUrunlerSortColumn == e.SortExpression)
            {
                if (EnCokSatilanUrunlerSortDirection)
                    EnCokSatilanUrunlerSortDirection = false;
                else if (!EnCokSatilanUrunlerSortDirection)
                    EnCokSatilanUrunlerSortDirection = true;
            }
            else
                EnCokSatilanUrunlerSortDirection = true;

            EnCokSatilanUrunlerSortColumn = e.SortExpression;

            EnCokSatilanUrunlerPageIndex = 0;

            GridView_EnCokSatilanUrunler.SelectedIndex = -1;


        }

        void EnCokSatilanUrunlerGridDoldur()
        {
            GridView_EnCokSatilanUrunler.DataSource = DAL.raporx.DAOUrunx.GetEnCokSatilanBesUrun(
                                                                                                    DateTime.Now - new TimeSpan(30, 0, 0, 0),
                                                                                                    DateTime.Now
                                                                                                );

            GridView_EnCokSatilanUrunler.DataBind();
        }

I just want a GridView without a SelectButton.

Upvotes: 1

Views: 22808

Answers (3)

Bachask8
Bachask8

Reputation: 428

to remove select for a certain row :

in RowDatBound

e.Row.Cells[0].Controls[0].Visible = false;

Upvotes: 2

deostroll
deostroll

Reputation: 11975

Tell the Gridview not to render the select button

gv.AutoGenerateSelectButton = false;

Next, add a template field. And add a link button inside it.

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnSelect" Command="cmdSelect" Text="Select" runat="server"/>
</ItemTemplate>
</asp:TemplateField>

You need handler for gridview's RowDataBound event. You can do this in markup or code. I've shown how to do this via code:

gv.RowDataBound += new EventHandler(OnRowDataBound);

The event sink is define as follows:

void OnRowDataBound(object sender, RowDataBoundEventArgs e)
{
    if(e.Row.RowType == RowType.Row)
    {
       LinkButton lbtnSelect = (LinkButton) e.Row.FindControl("lbtnSelect");
       //now hide or show as per you logic
    }
}

ps: Wrote code in haste, did not aim for correctness. Hence parts of code may be incorrect. If you google you might find the correct code for the above. Will edit later when I have time...

Upvotes: 1

Amir Ismail
Amir Ismail

Reputation: 3883

as long as you don't need the SelectButton try to remove it from Columns section and remove OnSelectedIndexChanged from your GridView declaration and delete GridView_EnCokSatilanUrunler_SelectedIndexChanged method from code behind.

Upvotes: 1

Related Questions