I need to show some texts on the DataGridViewButtonColumn. I've read lots of similar question about these on SO. Many of them answers recommend setting UseColumnTextForButtonValueto True which doesn't for me. It seems in an odd way that Microsoft makes it to have at least a row so that the button will display the text.
The following is my code:
DataGridViewButtonColumn EditColumn = new DataGridViewButtonColumn();
EditColumn.HeaderText = "Complete";
EditColumn.Name = "Complete";
EditColumn.UseColumnTextForButtonValue = True;
dataGridView.Columns.Add(EditColumn);
This code doesn not show the text on the DataGridViewButtonColumn:
The code that works here:
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "a";
dataGridView1.Columns[1].Name = "b";
ArrayList row = new ArrayList();
row.Add("1");
row.Add("2");
dataGridView1.Rows.Add(row.ToArray());
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
btn.Name = "text";
btn.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Add(btn);
This code works but however I am getting my data from a database and thus I won't be using dataGridView.Rows.Add(row.ToArray()) here. So how do I get the text to show on the DataGridViewButtonColumn ?
You have two problems to solve:
How bind a Button column to a DataSource
How to make the DataSource create not only default column types but also one (or more) button column
The first issue is basically solved by setting the DataPropertyName of the column.
But I do not know how or even if it is possible to influence the ColumnTypes created automatically.
Therefore I suggest a workaround: Instead of relying on the AutoGenerateColumns we can do it ourselves in a little helper function:
This clears the columns and creates a new one for each column in a DataTable, using the properties of the columns in the table. To tell it which column(s) we want to be buttons we pass a string with the column names, surrounded by spaces:
void CreateSpecialColumns(DataTable dt, DataGridView dgv, string buttons)
{
dgv.Columns.Clear();
for (int i = 0; i < dt.Columns.Count; i++ )
{
DataColumn dc = dt.Columns[i];
if (buttons.Contains(" " + dc.ColumnName + " "))
{
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.HeaderText = dc.Caption;
buttonColumn.Name = dc.ColumnName;
buttonColumn.ValueType = dc.DataType;
buttonColumn.DataPropertyName = dc.ColumnName;
dgv.Columns.Add(buttonColumn);
}
else
{ // normal columns
int c = dgv.Columns.Add(dc.ColumnName, dc.Caption);
dgv.Columns[c].ValueType = dc.DataType;
dgv.Columns[c].DataPropertyName = dc.ColumnName;
}
}
}
Here is how I called it using a DataTable DT:
dataGridView1.AutoGenerateColumns = false;
CreateSpecialColumns(DT, dataGridView1, " Cook Waiter ", "");
dataGridView1.DataSource = DT;
Note that this only works if the UseColumnTextForButtonValue property is not set to true!
You need to set the Text property,
DataGridViewButtonColumn EditColumn = new DataGridViewButtonColumn();
EditColumn.HeaderText = "Complete";
EditColumn.Name = "Complete";
EditColumn.UseColumnTextForButtonValue = True;
EditColumn.Text = "Complete"; // --Text property added
dataGridView.Columns.Add(EditColumn);
Related
I am creating a page which shows some details in several gridviews. I am saying several because the number of gridviews is not constant. I am having a panel on the aspx page and adding gridviews to it dynamically.
aspx code:
<asp:Panel ID="pnlResult" runat="server"></asp:Panel>
aspx.cs code
int numOfGroups = some number here;
for (int i = 1; i < numOfGroups + 1; i++)
{
GridView grd = new GridView();
grd.ID = "GridView" + i.ToString();
grd.BackColor = getColor(i);
grd.DataSource = dt; // some data source
grd.DataBind();
pnlResult.Controls.Add(grd);
}
But my problem is that the gridviews are adding one below the another . I want them to be side by side. How can I achieve that?
Note: Panel is not mandatory. Anything else can be used in its place
You have to float your elements inside the panel to left. To achieve that in your code behind, before adding grid to the panel do:
grd.Attributes.Add("class", "float-left");
Where float-left class in your css is defined as:
.float-left {
float: left;
}
So your code would look like:
for (int i = 1; i < numOfGroups + 1; i++)
{
GridView grd = new GridView();
grd.ID = "GridView" + i.ToString();
grd.BackColor = getColor(i);
grd.Attributes.Add("class", "float-left"); //here
grd.DataSource = dt; // some data source
grd.DataBind();
pnlResult.Controls.Add(grd);
}
If you use a table control, then you can create a row, and add each grid as cells, which would enforce the side-by-side requirement:
TableCell cell = new TableCell();
cell.Controls.Add(grd);
table.Rows(0).Cells.Add(cell);
Something like that; not sure if the Table API example above is 100% correct, but hopefully you get the idea.
Maybe u need just to add some css?
like that:
grd.DataSource = dt; // some data source
grd.Style["float"] = "left"; // css
grd.DataBind();
You can do something like this
Table tbl = new Table();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
btn = new Button();
btn.Text = "Add ";
btn.Height = Unit.Pixel(30);
btn.Width = Unit.Pixel(100);
tc.Controls.Add(btn);
tr.Controls.Add(tc);
tbl.Controls.Add(tr);
panel.Controls.Add(tbl);
When sorting the datagridview by clicking on any of my headers my button text disappears
var MoreInfoCol = new DataGridViewButtonColumn();
MoreInfoCol.Name = "MoreInfoColumn";
MoreInfoCol.HeaderText = "Extended Description";
dataGRID.Columns.Add(MoreInfoCol);
foreach (DataGridViewRow row in dataGRID.Rows)
{
row.Cells["MoreInfoColumn"].Value = row.Cells[2].Value.ToString() + " | More Info";
dataGRID.CellContentClick += new DataGridViewCellEventHandler(dataGRID_CellContentClick);
}
I figured it out
MoreInfoCol.UseColumnTextForButtonValue = true;
MoreInfoCol.Text = "My Value Goes here";
class TextColumn : ITemplate
{
private string controlId;
private string cssClass;
public TextColumn(string id, string cssClass = "inputFromTo")
{
controlId = id;
this.cssClass = cssClass;
}
public void InstantiateIn(Control container)
{
TextBox txt = new TextBox();
txt.ID = controlId;
txt.CssClass = cssClass;
container.Visible = true;
container.Controls.Add(txt);
}
}
/************************************ Add column code snippet ****************************/
TemplateField dentry = new TemplateField();
TemplateField dexit = new TemplateField();
TemplateField dslack = new TemplateField();
dentry.ItemTemplate = new TextColumn("txtHH" + nameCount + "DEntry");
dexit.ItemTemplate = new TextColumn("txtHH" + nameCount + "DExit");
dslack.ItemTemplate = new TextColumn("txtHH" + nameCount + "DSlack");
gvOfcBlowingReport.Columns.Insert(startPoint, dentry);
gvOfcBlowingReport.Columns.Insert(startPoint + 1, dexit);
gvOfcBlowingReport.Columns.Insert(startPoint + 2, dslack);
/********************************* Remove column code snippet ************************/
gvOfcBlowingReport.Columns.RemoveAt(startPoint - 1);
gvOfcBlowingReport.Columns.RemoveAt(startPoint - 2);
gvOfcBlowingReport.Columns.RemoveAt(startPoint - 3);
// after executing this code all the columns vanish.
Anyone know how to remove this text box template column?
In the above code I am adding templated field textbox and later on removing the same on button click but due to some reason all the templated field are getting affected and returning null when i am using FindControl in grid. In display also grid is displayed as empty.
Some other people are also facing the same problem over http://forums.asp.net/t/1162011.aspx but so far no valuable solution.
i want to add textboxes dynamically in C# on a button click in a table row. for that i have used the following code.
[1]: http://pastie.org/7702237.
the problem is i am able to adding as many textboxes as I want but they are adding at same location, i mean the table row is not incrementing for every button click instead it is simply replacing the old table row with the new one. Please help me in how to solve this problem.
Thanks in advance
Ganesh
Web-based applications do not maintain state; to this end the state of the table and any variables is not being maintained. With each postback (generated by the button), the table's state reverts to what it was prior to adding a row and then a row is programatically added to it.
In order to achieve your goal, you will need to maintain state somehow. In the following code snippet I am making use of a session:
private List<TableRow> TableRows
{
get
{
if(Session["TableRows"] == null)
Session["TableRows"] = new List<TableRow>();
return (List<TableRow>)Session["TableRows"];
}
}
The following is your code modified to work with the session variable:
TextBox txtE, txtM, txtB;
Button btnAdd, btnDel;
TableRow trow;
TableCell tcell;
foreach(TableRow tr in TableRows)
tblEduDetails.Controls.Add(tr);
int count = TableRows.Count + 1;
txtE = new TextBox();
txtE.ID = "E" + count.ToString();
txtE.Visible = true;
txtE.Text = "E " + count.ToString();
txtE.BorderWidth = 2;
txtE.TextMode = TextBoxMode.SingleLine;
txtE.Height = 30;
txtM = new TextBox();
txtM.ID = "M" + count.ToString();
txtM.Visible = true;
txtM.Text = "M " + count.ToString();
txtM.TextMode = TextBoxMode.SingleLine;
txtM.Height = 30;
txtB = new TextBox();
txtB.ID = "E" + count.ToString();
txtB.Visible = true;
txtB.Text = "B " + count.ToString();
txtB.TextMode = TextBoxMode.SingleLine;
txtB.Height = 30;
btnAdd = new Button();
btnAdd.ID = "A" + count.ToString();
btnDel = new Button();
btnDel.ID = "D" + count.ToString();
trow = new TableRow();
trow.ID = "R" + count.ToString();
trow.BorderWidth = 1;
tcell = new TableCell();
tcell.ID = "E" + count.ToString();
tcell.Controls.Add(txtE);
trow.Controls.Add(tcell);
tcell = new TableCell();
tcell.ID = "B" + count.ToString();
tcell.Controls.Add(txtM);
trow.Controls.Add(tcell);
tcell = new TableCell();
tcell.ID = "M" + count.ToString();
tcell.Controls.Add(txtB);
trow.Controls.Add(tcell);
tblEduDetails.Controls.Add(trow);
TableRows.Add(trow);
I have created an DataGridViewImage column for my DataGridView.
DataGridViewImageColumn guardar = new DataGridViewImageColumn();
{
guardar.HeaderText = "";
guardar.Name = "guardar";
guardar.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
guardar.CellTemplate = new DataGridViewImageCell();
guardar.Image = Properties.Resources.empty_grilla;
guardar.ToolTipText = "Guardar";
}
dgv_Bancos.Columns.Add(guardar);
The problem is that when I want to change the Image of an specific cell, it just does not changes.
This is my code, please help, I donĀ“t know what is wrong
DataGridViewImageCell cell = (DataGridViewImageCell)dgv_Bancos.Rows[e.RowIndex].Cells["guardar"];
cell.Value = Properties.Resources.guardar_grilla;
RESOLVED!
You can use like this instead of using the DataGridViewImageCell cell
dataGridView1["guardar", e.RowIndex].Value = Properties.Resources.guardar_grilla;