我想在任何列的最后一个单元格中添加按钮。这不起作用。我该怎么做?
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewButtonCell button = (row.Cells["1"] as DataGridViewButtonCell);
}
编辑:我不想添加专栏。我想在任何列上添加这些按钮'最后一个单元格,而不是行。
答案 0 :(得分:4)
您必须为要装饰的每个列创建<%= link_to 'Request for inventory', inventory_status_inventory_path(@inventory),
:class => 'btn btn-success' %>
:
DataGridViewButtonCell
然后,您可以将原始DataGridViewButtonCell cell_1 = new DataGridViewButtonCell();
替换为Cells
:
DataGridViewButtonCells
请注意,您需要为每个按钮创建单独 dataGridView1[oneOfYourButtonColumnIndices , buttonRowIndex] = cell_1;
!每个只能添加一次!
您可以根据需要设置Cell的样式:
Cells
您无法注册任何cell_1.Value = "Conquer";
cell_1.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
,因为events
类型不是DataGridViewCell
且未实施事件模型。
相反,必须在Control
中处理所有事件。这是一个例子:
DataGridView
您可以看到我可以像平常一样访问单元格的值。它也显示为按钮的文本。
请注意,所有单元格都有List<int> YourButtonColumnIndices = new List<int> {3,5,7};
private void DGV_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cell = DGV[e.ColumnIndex, e.RowIndex];
// check if we have hit a button cell..
if (YourButtonColumnIndices.Contains(e.ColumnIndex) && cell is DataGridViewButtonCell )
{
Console.WriteLine(cell.Value.ToString());
// do things that depend on the button:
// if (cell.Value == "Conquer") doConquer(..);
// else (cell.Value == "Command") doCommand(..);
// ..
}
}
属性,您可以在其中存储您喜欢的任何内容,包括委托!
另请注意,在模型中,插入或删除行时按钮的行可能会发生变化。因此,只有在添加按钮或使其保持最新时才应注意Tag
。我没有在buttonRowIndex
事件中检查它,因为列索引加上单元格类型应该足够了。如果不是,您还可以检查值..
当然,您可以根据需要对Click
方法进行编码,并根据需要添加参数。
顺便说一句:常规按钮总是doCommand
或您的系统对其配色方案中的按钮所具有的任何内容。但是你可以使用Silver
按钮,然后使用FlatStyle
(和/或Cell's Backcolor
):
Forecolor
如果您有两个或三个以上的按钮,您可能需要创建一个 cell_1.FlatStyle = FlatStyle.Flat;
cell_1.Style.BackColor = Color.LightSkyBlue;
来管理它们,可能是这样的:
Dictionary
并使用它来创建它们:
Dictionary<int, string> buttonList = new Dictionary<int, string>();
buttonList.Add(3, "Save Operator");
buttonList.Add(5, "Save An.1");
buttonList.Add(7, "Save An.2");
并在foreach(int i in buttonList.Keys)
{
DataGridViewButtonCell cell= new DataGridViewButtonCell();
cell.Value = buttonList[i];
cell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1[i, buttonRowIndex] = cell;
}
:
CellClick
答案 1 :(得分:0)
更新:
由于问题现在已更新,并且要求将按钮添加到<%@ Page Title="Fragebogen generieren" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Generate.aspx.cs" Inherits="MAXXREC.Generate" SmartNavigation="True" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %>.</h2><br /> <br />
<asp:Panel id="pCustomize" runat="server"></asp:Panel> <br /><br />
<asp:Button id="btnSave" class="btn btn-default" Text="Save" runat="server" OnClick="btnSave_Click"></asp:Button>
</asp:Content>
而不是按钮列。
private bool SelectTheData()
{
dtQuestionBlock = taQuestionBlock.GetData();
try
{
int rows = dtQuestionBlock.Rows.Count;
for (int i = 0; i < rows; i++)
{
UpdatePanel updatePanel = new UpdatePanel();
updatePanel.ID = "up" + dtQuestionBlock.Rows[i][1].ToString();
Label lbl = new Label();
lbl.ID = "lbl" + dtQuestionBlock.Rows[i][1].ToString();
lbl.CssClass = "h4";
lbl.Attributes.Add("runat", "server");
lbl.Text = dtQuestionBlock.Rows[i][1].ToString();
pCustomize.Controls.Add(lbl);
pCustomize.Controls.Add(new Literal() { ID = "br" + i, Text = "<br /><br />" });
HtmlTable tbl = new HtmlTable();
tbl.Width = "100%";
tbl.Attributes.Add("class", "table");
tbl.ID = "htmltbl" + dtQuestionBlock.Rows[i][1].ToString();
HtmlTableRow htr = new HtmlTableRow();
HtmlTableCell hcella = new HtmlTableCell();
CheckBox acb = new CheckBox();
acb.ID = "cb" + dtQuestionBlock.Rows[i]["name"].ToString();
//acb.CheckedChanged += new EventHandler(cb_CheckedChanged);
hcella.Width = "30px";
hcella.Controls.Add(acb);
htr.Cells.Add(hcella);
HtmlTableCell hcellf = new HtmlTableCell();
hcellf.InnerText = "Frage";
hcellf.Style.Add("font-weight", "bold");
hcellf.Style.Add("font-size", "15px");
htr.Cells.Add(hcellf);
tbl.Rows.Add(htr);
string cont = dtQuestionBlock.Rows[i]["ID"].ToString();
dtQuestion = taQuestion.GetDataBy1(Convert.ToInt32(cont));
nCountTables = i;
for (int j = 0; j < dtQuestion.Rows.Count; j++)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
acb = new CheckBox();
acb.ID = "cb" + dtQuestion.Rows[j]["content"].ToString();
cell.Width = "30px";
cell.Controls.Add(acb);
tr.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = dtQuestion.Rows[j]["content"].ToString();
cell.ID = "cell" + j + "_" + dtQuestion.Rows[j]["content"].ToString();
tr.Cells.Add(cell);
tbl.Rows.Add(tr);
}
updatePanel.ContentTemplateContainer.Controls.Add(tbl);
//tbl.Visible = false;
pCustomize.Controls.Add(updatePanel);
pCustomize.Controls.Add(new Literal() { ID = "br" + i + rows, Text = "<br />" });
}
return true;
}
catch (Exception ex)
{
Type cstype = ex.GetType();
ClientScriptManager cs = Page.ClientScript;
String cstext = ex.ToString();
cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
return false;
}
finally
{
taQuestionBlock.Dispose();
dtQuestionBlock.Dispose();
}
}
万一,如果你需要一个按钮列,代码将有所帮助。
DataGridViewButtonColumn
就是您所需要的。
cell
答案 2 :(得分:0)
You could employ a template field instead of a bound field. This would enable you to place a footer inside the field.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnEdit" CommandName="Edit" Text="Edit" runat="server" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnNew" CommandName="New" Text="New" runat="server" CommandArgument='<%# Container.DataItemIndex %>' />
</FooterTemplate>
</asp:TemplateField>
And if I remember correctly, this is then handled in the rowcommand event (therefore the command name and arguement).