【问题标题】:How to add a delete command button in devExpress gridview from codebehind?如何从代码隐藏中在 devExpress gridview 中添加删除命令按钮?
【发布时间】:2012-12-31 09:34:21
【问题描述】:

我使用的是 DevExpress gridview,里面有一个命令按钮。我想使用图像而不是它。怎么可能?

到目前为止,我已经尝试了这么多:

GridViewCommandColumn commancol = new GridViewCommandColumn(); 
commancol.ButtonType = ButtonType.Image; 
GridViewCommandColumnButton moveUpBtn; 
moveUpBtn.Image.Url = "~/App_Themes/Images/GridControl/grid-delete.png"; 
commancol.Add(moveUpBtn); 

但它给出了错误。

我想从 .cs 页面而不是 aspx 页面添加下面的方式按钮。所以我试图从代码隐藏中进行上述方式。

<dx:GridViewCommandColumn ButtonType="Image">
            <HeaderTemplate>
            </HeaderTemplate>
            <DeleteButton Visible="true" Text="" Image-Url="grid-delete.png"
                Image-ToolTip="Delete Record">
            </DeleteButton>
        </dx:GridViewCommandColumn>

谢谢

【问题讨论】:

  • 我认为您需要在代码后面的 gridview 上添加一个图像按钮
  • 我正在尝试使用下面的代码,但它在“commancol.Add(moveUpBtn);”上给了我错误行 GridViewCommandColumn commancol = new GridViewCommandColumn(); commancol.ButtonType = ButtonType.Image; GridViewCommandColumnButton moveUpBtn; moveUpBtn.Image.Url = "~/App_Themes/Images/GridControl/grid-delete.png"; commancol.Add(moveUpBtn);那么如何从.cs页面添加删除命令按钮..
  • 我只是想从后面的代码中添加一个删除命令按钮.. 那我该怎么做呢?
  • @sejalpatel 请粘贴 gridview 的设计以及 cs 页面代码,以便了解您在 gridview 中添加图像而不是命令按钮时遇到的问题。
  • @Dev 我粘贴了我想从代码隐藏而不是 aspx 页面添加的 gridview 按钮。

标签: c# asp.net devexpress


【解决方案1】:

您可以像下面这样简单地添加CommandColumn,但您应该注意asp.net页面事件以使其正确工作。

protected void Page_Init(object sender, EventArgs e)
{
    GridViewCommandColumn column = new GridViewCommandColumn("....");
    column.DeleteButton.Visible = true;
    column.DeleteButton.Image.Url = "set path here";
    column.Visible = true;
    column.VisibleIndex = 2;//place where you want it to display
    ASPxGridView1.Columns.Add(column); 
}
protected void Page_Load(object sender, EventArgs e)
{
   // Bind Data here
    BindData();
}

参考Command Columns的文档,然后你也可以获得另一个命令按钮,并在运行时根据需要设置它们的属性。

希望对您有所帮助..

【讨论】:

  • 谢谢..我想在网格中显示图像并显示删除链接
  • @sejalpatel 检查文档链接..它将帮助您更进一步
【解决方案2】:

你可以这样使用它, 1)在itemtemplate中声明imageButton 2) 声明commandnamecommandargument 3)声明getimagepath 4) 在cs页面getimage函数声明为

     <asp:TemplateField HeaderText="Edit" ItemStyle-VerticalAlign="Top">
    <ItemTemplate>
   <asp:ImageButton ID="ibDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("") %>'
                            ImageUrl='<%#GetImagePath("delete.png")%>' Width="20" Height="20" OnClick="ibDelete_Click"
                            OnClientClick='<%# Eval("", "return confirm(\"Are you sure to delete - {0} ?\");") %>'
                            ToolTip="Delete Item" />
    </ItemTemplate>




public string GetImagePath(string imgName)
        {
            string Finalurl =  this.TemplateSourceDirectory + "/images/" + imgName;
            return Finalurl;
        }

5) 像这样声明行命令

protected void row_command(object sender, gridviewrowcommand e)
{
     if(e.commandname=="Delete")
      {
           if(e.commandargument!=null)
            {
                  // do the work here
            }
       }
}

【讨论】:

    【解决方案3】:

    我想从后面的代码中添加一个以下类型的删除命令按钮。而不是从 aspx 页面。

    <dx:GridViewCommandColumn ButtonType="Image">
                    <HeaderTemplate>
                    </HeaderTemplate>
                    <DeleteButton Visible="true" Text="" Image-Url="grid-delete.png"
                        Image-ToolTip="Delete Record">
                    </DeleteButton>
                </dx:GridViewCommandColumn>
    

    【讨论】:

      【解决方案4】:
      <dx:GridViewCommandColumn Name="deleteRow" ShowDeleteButton="True" VisibleIndex="3" ButtonType="Image">
                      <CustomButtons>
                          <dx:GridViewCommandColumnCustomButton Visibility="AllDataRows" Image-Width="20" Image-Url="~/img/delete-icon.png"
                              Image-Height="20">
                              <Image Height="20px" Width="20px">
                              </Image>
                          </dx:GridViewCommandColumnCustomButton>
                      </CustomButtons>
                      <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle"></HeaderStyle>
                  </dx:GridViewCommandColumn>
      

      【讨论】:

        猜你喜欢
        • 2011-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多