How to get column name on image button clicked in column header? - c#

I have a WPF application where I need to work on datagrid ,in column header I have added image button to each column which when clicked should pop up with that specific column name and other details.
Currently I have done this:
void data1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
EventManager.RegisterClassHandler(typeof(Button), Button.ClickEvent, new RoutedEventHandler(btnFilterImage_Click) );
}
private void btnFilterImage_Click(object sender, RoutedEventArgs e)
{
viewModel.OnYieldDistinctValues(reportDatagrid.Columns.ToString());
lstYield.ItemsSource = viewModel.TextFilters;
popYield.Placement = PlacementMode.MousePoint;
popYield.IsOpen = true;
}
My question is how can I get the particular column name in btnFilterImage_Click.
Please help.!!
Thanks!!

Cast sender to Button and find its VisualParent using FindVisualParent till you get column.

Related

Having difficulty with DataGridView hyperlink on c# and ms accesss

I have winform in c# that contains datagridview that display table from my database. One of the column is hyperlink data type. But the link doesn't seem to display correctly. for example http://google.com display as #http://google.com# on the column. The question is:
How do I remove # from my hyperlink column?
How can I make the link accessible? I mean, whenever I click, the link opens in a browser. Here is the example pic
You need to set the link column separately.
I write a working code example, and you could have a look.
Code:
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewLinkColumn col1 = new DataGridViewLinkColumn();
dataGridView1.Columns.Add(col1);
dataGridView1.Columns[0].Name = "Links";
DataGridViewRow dgvr = new DataGridViewRow();
dgvr.CreateCells(dataGridView1);
DataGridViewCell linkCell = new DataGridViewLinkCell();
linkCell.Value = #"http:\\www.google.com";
dgvr.Cells[0] = linkCell;
dataGridView1.Rows.Add(dgvr);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewLinkColumn && !(e.RowIndex == -1))
{
Process.Start(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}

how to remove or disable the extra column and row selection function?

I have grid view, and I have already disable the column auto width so I can manually set the size of column. after manually resize there's a extra blank column. and what I want is :
To disable the select row function when i click on outside of the column in Active, code and generate
Remove the extra column.
I already success fully hide the remaining column with this event or code
private void gridView1_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == null)
{
e.Handled = true;
}
}
The thing is I still can click on the outside of the genre, and the row selection still follow where I click
Handle the GridView.MouseDown event in the following way:
private void gridView1_MouseDown(object sender, MouseEventArgs e) {
GridView view = (GridView)sender;
var hi = view.CalcHitInfo(e.Location);
Console.WriteLine(hi.HitTest);
if (hi.InRow && !hi.InRowCell)
DXMouseEventArgs.GetMouseArgs(e).Handled = true;
}

How to get Row Id in RepositoryLookupEdit_ValueChanged event

I have gridcontrol that has a RepositoryLookupEdit in one of the columns. I can get the value of RepositoryLookupEdit after changed, but I dont know how to get the which row's RepositoryLookupEdit value changed. How can I get the Row ID?
With the code below, I can get the RepositoryLookupEdit value.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
}
Since repositoryItemLookUpEdit isn't restricted to GridControls you cannot get the row handle from this event. You however have other possibilities.
First, if the edit is done by the user, you can use the ColumnView.GetFocusedRow() method to get the current grid row.
If however the edit value is changed via code it will also be changed in the grid so you can now use the ColumnView.CellValueChanged event.
private void repositoryItemLookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
LookUpEdit edit = sender as LookUpEdit;
var row = edit.Properties.GetDataSourceRowByKeyValue(edit.EditValue);
gridRow = gridView.GetFocusedRow() as MyDataRow
}

Select and show selected row to label

So, when I click Select I want to show in a Label all datas that contain one row. I have managed to make this, except DropdownList. When I click "Select" it's just empty.
protected void GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Label1.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
Label2.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
Label3.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
}
P.S.: I have not done this programmatically. The only code I've wrote on .aspx.cs file is the code above.
Use this to find the value. its not showing the value because of template field control.
I have used the gridview control that you pasted in pastebin.
protected void GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
Label drpValue =
(Label)this.GridView1.Rows[e.NewSelectedIndex].Cells[1].FindControl("Label1");
Lbl1.Text = drpValue.Text;
Lbl2.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
Lbl3.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
}

changing data type of autogenerated column

I have a Gridview that auto generates columns...
The problem is, the data type are all imageurl.
when the gridview populates, the url is in label format.
I could define them as templatefield which works, but the requirement is to auto generate.
I read on MSDN of this, but i dont know how to proceed from there
private void BUChecker_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
}
You can alter this on its TemplateFields. See GridView controls'.
see this for your reference.
I think you use RowDataBound as follows In case you don't want to use template field
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//find your cell suppose it is Cell 0
string imgUrl=e.Row.Cells[0].Text;
Image img = new Imge();
img.ImageUrl =imgUrl;
e.Row.Cells[0].Controls.Add(img);
}
}

Categories

Resources