Devexpress NavigationBar Item Onclick to load Grid into GridControl - c#

I am a newbie to devexpress, I really need help on how to manage NavBarControl item. When Navbar item onclick event is fired I want to load a GridView into gridControl.
For example. Let say I have two item/link in Group A which are link 1 and link 2, when Group A - Link 1 is clicked I want to load gridview1 into gridControl1 and if Link 2 is clicked load gridView2 to gridControl
How can I achieve this?

When Navbar item onclick event is fired I want to load a GridView into gridControl.
Take a look at the NavBarControl.LinkClicked event. You can handle this event as follows(use the e.Link property to detect the specific link):
navBarControl1.LinkClicked += navBarControl1_LinkClicked;
//...
void navBarControl1_LinkClicked(object sender, NavBarLinkEventArgs e) {
if(e.Link.Item == navBarItem1)
gridControl1.MainView = gridView1;
if(e.Link.Item == navBarItem2)
gridControl1.MainView = cardView1;
}
Or you can handle the corresponding NavBarItem.LinkClicked event for the specific items:
navBarItem1.LinkClicked += navBarItem1_LinkClicked;
navBarItem2.LinkClicked += navBarItem2_LinkClicked;
//...
void navBarItem1_LinkClicked(object sender, NavBarLinkEventArgs e) {
gridControl1.MainView = gridView1;
}
void navBarItem2_LinkClicked(object sender, NavBarLinkEventArgs e) {
gridControl1.MainView = cardView1;
}

Related

How to get selected row index in devexpress gridcontrol?

I have devexpress gridcontrol which looks like that:
I have click event on this red X button:
private void delete_button_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
}
How can get there row index where this button is ?
You cannot access rows on GridControl, since this is just a container for the views.
As I can see from your picture you're using GridView. When you press the delete button, focused row changes and you can access it via FocusedRowHandle.
private void delete_button_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
var gv = myGridControl.MainView as GridView;
var index = gv.FocusedRowHandle;
gv.DeleteRow(index);
}
You can use the GridView.FocusedRowHandle property:
view.DeleteRow(view.FocusedRowHandle);

how to get the column index of the gridview

I have two image buttons in the GridView used for selection. They populate the textboxes. What I need is when i click imagebutton "Edit" it should populate the textboxes from the gridview and when the imagebutton "Delete" is pressed it does the same and disbale the textboxes too. The point is am not getting a logic of how to get the column indexes programmatically.
help me out with the condition.
the issue is just with the c# code. Here is the snippet which i have tried:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
var row = GridView1.SelectedRow;
if (condition)
{
_PropertyTitle.Text = row.Cells[1].Text;
_Address.Text = row.Cells[2].Text;
}
else
{
_PropertyTitle.Text = row.Cells[1].Text;
_PropertyTitle.Enabled = false;
_Address.Text = row.Cells[2].Text;
_Address.Enabled = false;
}
}
}
It is not very clear from question that what you are trying to achieve.But what i got is you want to write specify both image button click separately.
for that you have to use the RowCommand event.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
In this row command event according to CommandName (of button) you can manage the code for two buttons
Refer this link for Rowcommand event row command
One more link

c# listbox selected change row event works but row is not selected

I have a problem. I have three buttons of main categories and when you click one of these buttons some things appear in ListBox and the buttons with subcategories appear. If you click on subcategory, consequently different things appear in ListBox.
I have methods like that:
private void DisplayPeople(string category); //I use it ParentClicked and SubClicked
private void ParentClicked(object sender, EventArgs e); //for parent category
private void SubCatClicked(object sender, EventArgs e); //for subcategory
myListBox.SelectedIndexChanged+= new EventHandler(selectedIndexChange);
When you select sth in listbox then it should appear in my DataGridView and it works perfectly. However, when I click on the button and things appear in listbox, and I put sth like myListBox.ClearSelected(); or my.SelectedItem = null; I see NOTHING is selected at the begining but still SelectedIndexChange event works because it adds first row to my DataGridView. I have no idea why, could u help me?
MUCH MORE SHORTER:
In my program when you select sth in ListBox, it appears in DataGridView. When i set myListBox.ClearSelected(); or my.SelectedItem = null;, nothing is selected in the begining but SelectedIndexChange event works and first thing in listbox is added to DataGridView. I don't want that, I want it to appear in datagridview only when it is selected by the user.
NOW MY EVENT HANDLER LOOKS LIKE THAT:
private void selectedIndexChange(object sender, EventArgs e)
{
Person person = (Person)MyListBox.SelectedItem;
if (MyListBox.Items.Count > 0 && MyListBox.SelectedItems.Count > 0)
{
Basket.Add(person);
dataGridView1.DataSource = Basket;
}
}
PS.
I SOLVED THE PROBLEM. I did it that the thing from listbox is added to DataGridView when user clicks on ListBox and SelectedIndexChange event appears. But maybe there is simpler and prettier solution?
That's exactly how SelectedIndexChanged is supposed to work.
What you have to do is compare myListBox.SelectedIndex to -1 or myListBox.SelectedItem to null to see if something is actually selected in the ListBox.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged(v=vs.110).aspx
try like this
private void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (myListBox.Items.Count > 0 && myListBox.SelectedItems.Count > 0)
{
//Do something in DatagridView
}
else
{
//clear the gridview
}
}

How to call event when right click and select value from gridview cell?

I want to call event when right click then select value in gridview cell.
Is it possible to achieve this?
When right click the cell it will show one popup it contains some value using jquery I achieved this
If I select some value it will be placed in that cell that time I want to call event
Any ideas? Thanks in advance
If it happens when you click on Q1, then you could add onclick attribute to the popup item when you are populating it. Then you can use that event to do the work you need.
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
writer.AddAttribute("onclick", "YourFunction()")
base.AddAttributesToRender(writer);
}
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
// Add this
dataGrid.CurrentCell = dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
// Can leave these here - doesn't hurt
dataGrid.Rows[e.RowIndex].Selected = true;
dataGrid.Focus();
}
}
https://stackoverflow.com/a/16765616/1428854
or
Right click to select a row in a Datagridview and show a menu to delete it

The GridView 'GridView1' fired event RowDeleting which wasn't handled , but there is an event

ı have been traying to create dynamic control panel using webusercontrol , delegate,and ADO.
Even ı have write the delegate for deleting and editing ı faced with "The GridView 'GridView1' fired event RowDeleting which wasn't handled."problem . Can anybody help me pls
here is my codes
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = this.DataSource;
GridView1.DataBind();
GridView1.DataKeyNames = new string[] { this.DataKeyNames };
}
public object DataSource { get; set; }
public string DataKeyNames { get; set; }
public event GridHander RowDeleting;
public event GridHander RowSawing;
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow gvr = ((LinkButton)e.CommandSource).Parent.Parent as GridViewRow;
int rowIndex = gvr.RowIndex;
object id = GridView1.DataKeys[rowIndex].Value;
switch (e.CommandName)
{
case "Edit":
GridView1.EditIndex = rowIndex;
break;
case "Delete":
if (RowDeleting != null)
{
GridEventArgs args = new GridEventArgs()
{
row=gvr,
id=id,
rowIndex=rowIndex
};
RowDeleting.Invoke(e.CommandSource, args);
}
break;
case"Save":
if (RowSawing != null)
{
GridEventArgs args = new GridEventArgs()
{
row = gvr,
id = id,
rowIndex = rowIndex
};
RowSawing.Invoke(e.CommandSource, args);
}
GridView1.EditIndex = -1;
break;
case "Cancel":
GridView1.EditIndex = -1;
break;
default:
break;
}
}
}
//My webform
ublic partial class CategoryControlPanel : System.Web.UI.Page
{
CategoryResposite _categoryResposite=new CategoryResposite();
protected void Page_Load(object sender, EventArgs e)
{
ControlPanel.DataSource = _categoryResposite.ListCategories();
ControlPanel.RowDeleting += ControlPanel_RowDeleting;
ControlPanel.RowSawing += ControlPanel_RowSawing;
}
void ControlPanel_RowSawing(object sender, GridEventArgs e)
{
throw new NotImplementedException();
}
void ControlPanel_RowDeleting(object sender, GridEventArgs e)
{
_categoryResposite.DeleteCategories(Convert.ToInt32(e.id));
}
You are trying with a command name of Delete for your delete button. So the gridview creates a row deleting event automatically....
You need to change the command argument from Delete to something else like Delete_Product or whatever you want...
The code that you have posted is incomplete (missing the aspx file code), from your description of the problem it sounds as though you have not assigned the RowDeleting event to GridView1.
Inside the opening gridview tag in the aspx file add the assignment as follows:
<asp:gridview ID="..." runat="server" ... OnRowDeleting="<name of event handler>" ...>
If the event handler ControlPanel_RowDeleting is designed to handle a delete from gridview action then insert this as the event handler name. Ensure that you re-bind the gridview after delete so that the changes are shown on postback.
protected void ControlPanel_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// cancel the automatic delete action
e.Cancel = true;
// do the delete
_categoryResposite.DeleteCategories(Convert.ToInt32(e.id));
// complete delete action
GridView1.DataBind();
}
One of the good things about GridView is that it provides a built-in CommandField Buttons which allows us to perform certain actions like editing, updating,deleting and selecting of GridView data.
To add those command fields mentioned in the GridView you can follow these few steps below:
1. Switch to Design View
2. Right Click on the GridView and Select --> Show Smart Tag --> Add New Columns
3. On the List Select CommandField
4. Check Delete and Edit/Update options then OK
As you can see the Edit and Delete CommandField are automatically added in the last column of GridView. Now we can start to write our codes for editing and updating the information in the GridView.
In-order to perform Edit and Update in GridView we need to use three events ( GridView_RowEditing, GridView_RowCancelingEdit , GridView_RowUpdating). For those who do not know on how to generate Events in GridView you can follow these steps below:
Switch to Design View in Visual Studio Designer
Click on the GridView
Navigate to the GridView Property Pane and then SWITCH to Event Properties
From there you would be able to find the list of events including those three events mentioned above
Double Click on that to generate the Event handler for you
Try adding protected to the method signatures.

Categories