How to create button column in radgrid dynamically in asp.net?
I have a radgrid in My Page. I want to create button column in radgrid dynamically. I create columns and set command name for them.When I click on one of item and go to ItemCommand event , command name is null (""). Why command name is empty?
I have a data table that contain 3 column.
all columns the first , dynamically generate.
private void createColumnToGrid(DataTable dtData)
{
radGridTicketByFlagList.DataSource = new string[] { };
radGridTicketByFlagList.Columns.Clear();
for (int i = 0; i < dtData.Columns.Count; i++)
{
GridButtonColumn col = new GridButtonColumn();
col.ButtonType = GridButtonColumnType.LinkButton;
col.CommandName = "Total";
col.UniqueName = "TCFlag" + i;
col.HeaderText = dtData.Columns[i].ToString();
col.DataTextField = dtData.Columns[i].ToString();
radGridTicketByFlagList.Columns.Add(col);
}
}
Then fill data source of radgrid.
private void FillRptTicketByFlagMonthList()
{
//........
radGridTicketByFlagList.DataSource = new string[] { };
DataTable dtTicketFlag= clsReportManager.GetRptTicketByFlag(); // fill data table
radGridTicketByFlagList.DataSource = dtTicketFlag;
radGridTicketByFlagList.DataBind();
}
grid is generated. When I click on a item in grid (link button) , and go to ItemCommand event, e.CommandName is empty. Why??
I want to When I click on item , do an action for me.
Some time ago i had same problem, then i used this tip from telerik forum:
http://www.telerik.com/forums/commandname-is-empty-if-the-column-if-created-from-code
so sum up, you need to add to your grid Rebind() after creating button column.
Maybe there is some other way to make it works, but i don't know them :(
Related
I have a grid with some buttons. And basically want to edit the IsEnabled state of a Button in a specific Grid Row + Column. I don't get how to do this e.g.
public void Disablebutton (int Column, int Row)
{
//disable the button at Grid Row = Row and Grid Column = Column
}
Can someone provide an example how to solve given problem?
EDIT:
I found a temporary solution... Well, it's working, but it's very bad
if("B" + x.ToString()+ y.ToString() == "B00")
{
B00.IsEnabled = false; //B00 is the button name f.e.
}
I don't know if you're using WPF, Windows Forms or anything else, but you should have a Cell in the datagrid that you access using the row and col values. That cell has the button control that you can cast to a variable to change its properties. Something like this:
Button button = (Button)Grid.Cell[Column][Row].Control;
button.IsEnabled = false;
or
Button button = Grid.Cell[Column][Row].Control as Button
button.IsEnabled = false;
Keep in mind that this is a very rough example, since I don't know what technology you're using to paint the interface.
I'm having a slight problem getting a DataGridView to work properly. Currently, I have a DataSource to bind my data (in this case a List<T> of objects) and have the following method to bind the data to the control:
private void Bind(List<CAMPDomainLayer.FeeDefinition> fees)
{
BindingSource bs = new BindingSource(fees, null);
dgvFees.AutoGenerateColumns = false;
var colDesc = new DataGridViewTextBoxColumn
{
HeaderText = "Description",
DataPropertyName = "Description",
ReadOnly = true,
AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader
};
var colAmount = new DataGridViewTextBoxColumn
{
HeaderText = "Amount",
DataPropertyName = "FeeAmount",
ReadOnly = false,
};
var colDelete = new DataGridViewButtonColumn
{
HeaderText = string.Empty,
Text = "Delete",
};
dgvFees.Columns.AddRange(new DataGridViewColumn[] { colDesc, colAmount, colDelete });
dgvFees.DataSource = bs;
}
Notice, that I'm adding a button column for a delete button, I'm currently handling that with the following event:
private void dgvFees_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(dgvFees.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
{
int index = e.RowIndex;
dgvFees.Rows.RemoveAt(index);
dgvFees.Refresh();
CalculateTotal(Fees, PaymentAmount);
}
}
This seems to work fine. The dgvFees.Rows.RemoveAt(index); line removes the row from the control and the item from the underlying List<T>. The problem comes if I try to add a new row. Right now, I have a refresh button that grabs a list from the server and re-binds using the above Bind() method. The code to do that is:
Fees = fees;
Bind(Fees);
Where Fees is just a List<T>. When this is run, and I delete a row (right now there's) everything works fine. The row is deleted and the underlying list has nothing in it. But when I try to refresh (hence adding new rows), I get this:
The hat first row is duplicated, and appended horizontally to the first row. What stmpus me is right before we rebind, we can see that the DataSource only has one item:
Even after binding, I can inspect and see that there is still on 1 item in that datasource:
So my question is, why, if there is only one item in the DataSource, can I see a duplicate row. Is the old DataSource somehow "phantomed" in there? Is this an issue becuase of my biding approach (which admittedly, I don't know much about Winforms) or is this some bug in the framework?
I have a gridView, and inside is there exist a modifiable textbox.
I also have a dropdownlist, where if I click the dropdownlist, the system will read the database in SQL and display it in gridView.
How do I keep the number I previously typed in the first row of the gridView, when a new data is added after the dropdownlist is clicked?
Normally, the previously entered value of the textbox will be resetted if i rebind the gridView witthout refilling the textbox with a saved list or behaviour
Thank you very much in advance
I'm assuming you are using something like the OnSelectedIndexChanged event of the DropDownList. In there you can use FindControl to find the TextBox, store the data, rebind the GridView and set the value back to the TextBox. You need to use FindControl twice, one for the old and once for the new TextBox.
Note the use of oldValues.Count instead of GridView1.Rows.Count. If there were less rows in the new dataset than in the old you would get Index out of Bounds error.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
List<string> oldValues = new List<string>();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
TextBox tbOld = GridView1.Rows[i].FindControl("TextBox1") as TextBox;
oldValues.Add(tbOld.Text);
}
//rebind gridview here
for (int i = 0; i < oldValues.Count; i++)
{
TextBox tbNew = GridView1.Rows[i].FindControl("TextBox1") as TextBox;
tbNew.Text = oldValues[i];
}
}
You have used dropdownlist and textbox inside gridview?
if you click dropdownlist select value then postback to server and textbox value reset?
I have a XtraGrid GridControl defined with 3 columns, 2 databound and one I have set to a RepositoryItemComboBox. The column is setup like:
this.gridColumn3.Caption = "Test";
this.gridColumn3.FieldName = "test";
this.gridColumn3.Name = "gridColumn3";
this.gridColumn3.UnboundType = DevExpress.Data.UnboundColumnType.String;
this.gridColumn3.Visible = true;
this.gridColumn3.VisibleIndex = 2;
The RepositoryItemComboBox is created like:
RepositoryItemComboBox cbo = new RepositoryItemComboBox();
cbo.Items.Add("1");
cbo.Items.Add("2");
cbo.Items.Add("3");
cbo.Items.Add("4");
cbo.Items.Add("5");
cbo.Items.Add("6");
cbo.Items.Add("7");
gridView1.Columns[3].ColumnEdit = cbo;
When viewing the grid, the combobox displays exactly as I want it. This issue is when trying to retrieve the value selected in the combobox. When a button, outside of the grid, is pressed the combobox value should be processed. I use the following code:
for (int i = 0; i < gridView1.DataRowCount; i++)
{
int ID = (int)gridView1.GetRowCellValue(i, "id");
string Test = gridView1.GetRowCellValue(i, "test").ToString();
ProcessCombo(ID, Test);
}
In the above code ID is retrieved as expected, but gridView1.GetRowCellValue(i, "test") returns null. What could I have missed out? Is this even the right way to approach this?
ComboBox editor is only exists when the focused cell is in editing state. So, ComboBox is created when you activate it and is destroyed when you go away. So, when your Button is pressed there are no ComboBox in your GridView.
If you want to get the ComboBox value then you need to use events of your RepositoryItemComboBox or editing events of your GridView. The link to the editor is stored in sender parameter of RepositoryItemComboBox events and in GridView.ActiveEditor property. The value of the ComboBox you can get by using ComboBox.EditValue property or by using GridView.EditingValue property. Also you can get the value by using EventArgs parameter of GridView editing events like ValidatedEditor, CellValueChanging, CellValueChanged.
For example:
private void RepositoryItemComboBox1_Closed(object sender, ClosedEventArgs e)
{
//Get the value from sender:
var comboBox = (ComboBoxEdit)sender;
object value = comboBox.EditValue;
//Get the value from active editor:
object activeEditorValue = gridView1.ActiveEditor.EditValue;
//Get the value from editing value:
object editingValue = gridView1.EditingValue;
}
Also, if you want to store your value and use it later then you can use ColumnView.CustomUnboundColumnData event.
Here is example:
private Dictionary<int, string> _comboBoxValues = new Dictionary<int, string>();
private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
if (e.Column.FieldName != "test")
return;
int id = (int)gridView1.GetListSourceRowCellValue(e.ListSourceRowIndex, "id");
if (e.IsSetData)
_comboBoxValues[id] = (string)e.Value;
else if (e.IsGetData && _comboBoxValues.ContainsKey(id))
e.Value = _comboBoxValues[id];
}
I decided to change the column to a bound field, passed in an empty string when populating the grid, and now I can access the data via:
string Test = gridView1.GetRowCellValue(i, "test").ToString();
Didn't want to do this, but it works so...
I have 2 grids, grid1 and grid2.
grid2 will be filled based on which row is clicked in grid1. I have done it by binding OnSelectedIndexChanged of grid1.
But at page load the grid2 will be empty as no row selection is made.
So I was planning of firing the row selection of grid1 using c# code so that both grids will be having data at page load.
I have started coding like.
grid1.DataSource = versions.DefaultView;
grid1.SelectedIndex = 0;
grid1.DataBind();
But the event is not firing.
Can anyone help me in solving this issue?
You can just call the method programmatically.
grid1.DataSource = versions.DefaultView;
grid1.SelectedIndex = 0;
grid1.DataBind();
grid1_SelectedIndexChanged(grid1, new EventArgs());
You don't need the event on Page_Load because you already know what the selected index of the first grid should be. The event is needed after user interaction with the page. Just DataBind() the second grid on Page_Load which data corresponding to the 0 selected index of the first grid the same way that you databind the first grid.
if (!IsPostBack)
{
grid1.DataSource = versions.DefaultView;
grid1.SelectedIndex = 0;
grid1.DataBind();
DataBindGridByIndex(0);
}
else
{
grid1.DataSource = versions.DefaultView;
grid1.DataBind();
}
public void DataBindGridByIndex(int index)
{
// Logic to databind second grid by selected index.
}