Dynamic templates for listview/Radlistview - c#

I am trying to accomplish the task of loading a template into a listview depending on the data coming in and what the text in the field "Type" has in it.
At the state i am at the templates will load and perform as needed but not on the record i am currently am working with, it will produce the template on the next record instead.
protected void RadListView1_OnItemDataBound(object sender, RadListViewItemEventArgs e)
{
if (e.Item.ItemType == RadListViewItemType.DataItem || e.Item.ItemType == RadListViewItemType.AlternatingItem)
{
RadListViewDataItem dataItem = (RadListViewDataItem)e.Item;
DataRowView rowView = (DataRowView)dataItem.DataItem;
string Keyword = rowView["Type"].ToString();
if (keyword == "anothertest")
{
RadListView1.ItemTemplate = LoadTemplate("Cash.ascx");
}
else if (keyword == "test")
{
RadListView1.ItemTemplate = LoadTemplate("Credits.ascx");
}
}
}
this is how i am currently achieving this, do i need to complete this method on something other than "onItemDataBound" to work on the current record? Thanks in advance.

Related

How to convert cells of DataGridView to int?

I tried
int A = DataGridView1.Rows[0].Cells[3].value
and
int A = (int)DataGridView1.Rows[0].Cells[3].value
or using Cast or Parse method but it doesn't work.
You shouldn't be doing any of these things.
Your DataGridView should be databound (databind'd) to a DataTable that has a fourth column that is an integer or to a list of objects that has a property that appears in the fourth column of the datagridview, that is an integer:
var x = (int)myDataTableThatTheDGVIsBoundTo.Rows[0][3];
var x = myListOfPeopleWhereAgeAppearsAsDGVFourthColumn[0].Age;
DataGridView is intended for VIEWing a model of data that is kept elsewhere. If you want data out of the model, you get it from the model, not the view. If you want to know something that is eg what the user selected in the view, or is the current row etc you either retrieve the relevant model rows from the SelectedCell's OwningRow.DataBoundItem or use something like a bindingsource that understand sthe concept of "current row"
To really answer this we need a lot more info about how your data got into your grid in the first place. I get the feeling it's not in a model at all :/
try
{
string cellValue = DataGridView1.Rows[0].Cells[3].Value.ToString();
int intCellValue = System.Convert.ToInt32(cellValue);
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
The try-catch block is used in case the user somehow inserted a non-integer value in the datagrid.
Something like that will work too.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress);
if (dataGridView1.CurrentCell.ColumnIndex == 0) //Desired Column
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress);
}
}
}
private void Column1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
Found this code sample on another thread.
dgwProducts.Rows[0].Cells[3].Value
return object.
You can this:
int A = Convert.ToInt32(dgwProducts.Rows[0].Cells[3].Value.ToString());

Get data from DataGrid WPF

How can I get data from DataGrid when ComboBox' text is equal with the record of the DataGrid:
combobox1.ItemsSource = database.Mahs.ToList();
combobox1.DisplayMemberPath = "MahName";
and
private void datagrid_customer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var data = datagrid_customer.SelectedItem;
string id = (datagrid_customer.SelectedCells[0].Column.GetCellContent(data) as TextBlock).Text;
txt_f1.Text = id;
}
It shows me the id, but when i selected item but i want show me id when combobox.Text = name of the row in the DataGrid, then show the id of that row.
I would suggest changing your approach slightly, and retrieving the entire object from both controls for the comparison. This way you have full access to the properties of each selected object.
If the objects you're retrieving from your database override .Equals and .GetHashCode you could do away with some of the if statements below. But to get you started here's a quick example of your change listener
private void datagrid_customer_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Cast the objects from your DataGrid and your ComboBox.
YourDataObject dataItem = (YourDataObject)dataGrid1.SelectedItem;
YourDataObject comboItem = (YourDataObject)combo.SelectedItem;
// Compare your objects and decide if they're the same. Ideally you would
// have Equals and HashCode overridden so you could improve this
if (dataItem == null || comboItem == null)
text.Text = "Not Matching";
else
{
if (dataItem.MahName == comboItem.MahName)
// You've got full access to the object and all it's properties
text.Text = dataItem.Id.ToString();
else
text.Text = "Not Matching";
}
}

How to change RepositoryItem without using CustomRowCellEdit?

I would like to change RepositoryItem in diffrent way than shown in the code bellow. Motivation to do this is described in obsolete field CustomRowCellEditEventArgs.RowHandle.
private void GridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
GridView view = sender as GridView;
if (e.Column.FieldName == CONSTS_FIELD_NAME)
{
var val = (VAL_TYPE) view.GetRowCellValue(e.RowHandle, CONSTS_FIELD_NAME);
if (val == VAL_VALUE)
e.RepositoryItem = new RepositoryItem(); // setting new Repository Item
}
}
So I decided to use this code:
private void GridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
{
GridView view = sender as GridView;
if (e.Column.FieldName == CONSTS_FIELD_NAME)
{
var result = view.GetSelectedRows();
var val = (VAL_TYPE) view.GetRowCellValue(result.First(), CONSTS_FIELD_NAME);
if (val == VAL_VALUE)
e.RepositoryItem = new RepositoryItem(); // setting new Repository Item
}
}
Is there any other way to change RepositoryItem using some events?
The situation with CustomColumnDataEventArgs you mentioned(providing unbound data for the specific column) have no relation with GridView's editing process. When the CustomRowCellEdit event is fired all the row handles are already calculated. Thus there is no motivation to avoid the first approach.
The only recommendation I can suggest is to use the predefined repository item instead of creating a new one every time:
void GridView_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e) {
GridView view = sender as GridView;
if(e.Column.FieldName == CONSTS_FIELD_NAME) {
var val = (VAL_TYPE)view.GetRowCellValue(e.RowHandle, CONSTS_VAL);
if(val == VAL_VALUE)
e.RepositoryItem = repositoryItemForVAL_VALUE;
}
}
To use a specific editor for inplace-editing only, you should handle the CustomRowCellEditForEditing event.
Please also carefully read the Remarks section of the GridView.CustomRowCellEdit event documentation which clearly describes how this event works.

CellFormating get curent row

I am using DataGridView CellFormating to format specified cells.
The code I am trying:
private void dgwPart_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (this.arts.Type == ArtType.Pak)
{
if (dgwPart.Columns[e.ColumnIndex].Name == "Discount")
e.Value = "";
}
}
The problem is that it changes the value for all column, but I only want that it changes the value for specified row. How do I manage it?
You can use e.CurrentCell to find whether it is the row you want, something like:
If (dvImages.CurrentCell.RowIndex == 10)
{
Debug.WriteLine("Do something")
}
If ((int)(dvImages.CurrentCell.Value) = 100)
{
...
}
* these should be within the cellformat, after you check the column.
you may need check the syntax, I just typed this from my header.

Unable to find control in gridview

I want to find the control(hyperlink) in the gridview. Based on the value of the control I want to enable or disable the hyperlink.
I tried like this. But I am always getting null.
protected void gridResult_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink status = e.Row.FindControl("id") as HyperLink;
if ( status != null && status.Text == "AAAA" ) {
status.Enabled = false;
}
}
}
Please help.
Your "id" value is highly suspicious. My money is on the fact that you are supplying the wrong control name: FindControl("id!!!!!!!").
I would expect to see something like:
HyperLink status = e.Row.FindControl("hlStatus") as HyperLink;
If you are indeed supplying the correct control name (yuck), then it may be that your hyperlink control is nested too deep, in which case, you will need to 'crawl' your control hierarchy looking for it.
#dlev is absolutely correct, controls are often nested so you will need to create your own function of sorts to find what youre looking for, you could pass this function your control collection (e.Row.Controls()) and your id
private HyperLink FindControl(ControlCollection page, string myId)
{
foreach (Control c in page)
{
if ((HyperLink)c.ID == myId)
{
return (HyperLink)c;
}
if (c.HasControls())
{
FindControl(c.Controls, myId);
}
}
return null; //may need to exclude this line
}

Categories