Grabbing second column value of Datagrid - c#

Hi I'm trying to grab the second column value on click and to display in a text box but im really new to C#.
private void DataGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
display.Text = Datagrid.SelectedItem.ToString();
}
This displays all columns, but i only want second column, the second colummn header is Name.
var query = from loan in Loans
select new {Date = loan.StatusCommittedDate, Name = loan.PublicationName}
DataGrid.ItemsSource = query.ToList();

Based on how your datagrid is binded this may work
private void DataGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
int selectedIndex = dataGrid.SelectedIndex;
if (selectedIndex > -1)
{
DataGridColumn column = dataGrid.Columns[0];
Label lblName = (Label)column.FindControl("ControloftheIDwithPublicationNameBinded");
display.text = lblName.text;
}
}
Hope this helps

Related

Add tooltip to specific column on Data Grid View

I have a Data Grid View like:
if (this.dgv.Rows.Count < 1)
{
this.dgv.DataSource = null;
this.dgv.DataBindings.Clear();
if (this.dgv.Columns.Count == 0) this.dgv.ColumnCount = 15;
this.dgv.ColumnHeadersVisible = true;
this.dgv.Columns[4].Name = "Added By";
this.dgv.Columns[5].Name = "AddedByFullName";
}
as you can see I have column 4 called Added By
this.dgv.Columns[4].Name = "Added By";
and column 5 called AddedByFullName
this.dgv.Columns[5].Name = "AddedByFullName";
I want to know how can I use AddedByFullName column as a tooltip for Added By column then I will just remove AddedByFullName column , is that possible? Regards
You can do this using the CellMouseEnter or CellToolTipTextNeeded event for the DataGridView. Hide the column you want to use as the source, then replace the control name in the sample to match your DataGridView.
private void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if ((e.ColumnIndex == dgv.Columns["Added By"].Index)
&& (e.RowIndex > -1))
{
dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = dgv.Rows[e.RowIndex].Cells[dgv.Columns["AddedByFullName"].Index].Value.ToString();
}
}
Using CellMouseEnter event can be a posibility to achieve this but it also can be done with CellFormatting event as Microsoft REFERENCE
private void dgJobNotes_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if ((e.ColumnIndex == this.dgJobNotes.Columns["Added By"].Index)
&& e.Value != null)
{
dgJobNotes.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = dgJobNotes.Rows[e.RowIndex].Cells[5].Value.ToString();
}
The DataGridViewColumn class has a ToolTipText property. If you set it on the column, you will get a tool tip for the column header. If you want a tool tip to show up on every cell, you can implement a CellFormatting event handler, pull out the right cell (from the column) and set the cell's ToolTipText property. Something like:
private const int InterestingColumnNumber = 5;
private const string InterestingColumnToolTipText = "This Space For Rent";
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
var senderGridView = sender as DataGridView;
if (senderGridView != null)
{
if (e.ColumnIndex == InterestingColumnNumber)
{
var cell = senderGridView.Rows[e.RowIndex].Cells[InterestingColumnNumber];
cell.ToolTipText = InterestingColumnToolTipText;
}
}
}
There is a CellToolTipTextNeeded event which is created specifically for setting tooltip text. You don't need to use use CellFormatting or CellMouseEnter. If you are going to show text of column 5 as tooltip of column 4, you can write:
private void g_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
{
if (e.ColumnIndex == 4 && e.RowIndex >= 0)
{
e.ToolTipText = $"{dataGridView1[5, e.RowIndex].Value}";
}
}

How to make the new added unbound column value auto number become static?

First I make an unbound column in GridView (using DevExpress):
public void buttonl_Click(object sender, EventArgs e)
{
//GridColumn clmn = gridaaa.Columns["code"];
if (gridaaa.Columns.ColumnByFieldName("idx") != null)
{
gridaaa.Columns.ColumnByFieldName("idx").Dispose();
}
GridColunn unbColumn2 = gridaaa.Columns.AddField("idx");
unbColumn2.VisibleIndex = gridaaa.Columns.Count;
unbColumn2.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
//// Disable editing.
unbColumn2.OptionsColunn.AllowEdit = false;
// gridaaa.Columns.ColumnByFieldName("idx").Dispose();
}
In the gridView1_CustomUnboundColumnData event I am using this code
private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
if (e.Column.FieldName == "idx")
{
e.Value = e.ListSourceRowIndex;
}
}
for the output or result was:
The question is how to make the index or idx column value become static when I click sorting in any column header. As you can see when I click genre header the value follow with code or index in database, I want to make it like autonumber from 0 whenever I keep click column header of genre or anything:
You can use ColumnView.GetRowHandle method to get the handle of current row which is always reflects to the position of row in GridView from up to down.
Here is example:
private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
if (e.Column.FieldName == "idx")
e.Value = gridView1.GetRowHandle(e.ListSourceRowIndex);
}

datagridview combobox list color changed to black c#

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
int n = dataGridView1.CurrentCell.RowIndex;
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
var cbCell = dataGridView1.Rows[n].Cells["category"] as DataGridViewComboBoxCell;
DataTable dt = c1.ret("select category from category").Tables[0];
cbCell.DataSource = dt;
cbCell.ValueMember = "category";
cbCell.DisplayMember = "category";
cbCell.FlatStyle = FlatStyle.System;
}
}
i am trying to set datasource for datagrid combobox but when i set the datasource. the dropdown list of combobox color turns to black .i have tryed some code for setting the background color but every code failles .now i am stuck with my project. please help me....
You'll have to assign the default color to the combobox control.
After some research I found a solution, thanks to Nick.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//your code....
var cmbBx = e.Control as DataGridViewComboBoxEditingControl; // or your combobox control
if (cmbBx != null)
{
// Fix the black background on the drop down menu
e.CellStyle.BackColor = this.dgvSummary.DefaultCellStyle.BackColor;
}
}

How to fetch the column contents based on the row number

How to fetch column contents based on the row number (I have written the code to get the specific row number of the clicked cell.. all I want to do is retrieve all the column contents of that particular row) in data grid view using c#.
Here is my Code:
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit_1);
this.comboBox1.Size = this.dataGridView1.CurrentCell.Size;
this.comboBox1.Visible = true;
}
void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
this.comboBox1.SelectedIndex = 0;
this.comboBox1.Location = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
this.comboBox1.Size = this.dataGridView1.CurrentCell.Size;
this.comboBox1.Visible = true;
}
private void dataGridView1_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
{
int i;
if (this.comboBox1.SelectedItem != null)
{
//this.dataGridView1.CurrentCell.Value = this.comboBox1.SelectedValue;
this.dataGridView1.CurrentCell.Value = this.comboBox1.SelectedValue;
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
var value = (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].RowIndex.ToString());
MessageBox.Show(value); // right here i am getting the row number of clicked cell.
Assuming that you have a DataTable as the DataSource of dataGridView1
The below code gives the content of a cell if you know the row and column indices:
string data = ((DataTable)dataGridView1.DataSource).Rows[ROWINDEX].ItemArray[COLUMNINDEX].ToString();
The below code gives the content of a cell if you know the row index and the column name:
string data = ((DataTable)dataGridView1.DataSource).Rows[ROWINDEX]["COLUMNNAME"].ToString();

Datagridview ComboBoxCell set default value?

I have a datagridview with lots of data, and when I add a new line, the first column's last row creates a new ComboBoxCell which contain four items. But I can't set the default value ("DropDown") for the combobox. Every time I must manually select "DropDown". What is the solution?
DataGridViewComboBoxCell dgvCell = new DataGridViewComboBoxCell();
dgv[1, dgv.Rows.Count - 1] = dgvCell;
string[] controltype = {"DropDown", "CheckBoxList", "ListControl", "Tree" };
dgvCell.DataSource = controltype;
private void dataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells[4].Value = "DropDown";
}
it,s easy,If you have a ComboBox Column in your DataGrid View and you want to know what is the selected index of the combo box, then you need to do this:
1. Handle the EditingControlShowing event of DataGrid View. In this event handler, check if the current column is of our interest. Then we create a temporary ComboBox object and get the selected index:
Code
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
// Check box column
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}
void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = ((ComboBox)sender).SelectedIndex;
MessageBox.Show("Selected Index = " + selectedIndex);
}
try :
if(!isPostBack)
{
dgvCell.SelectedItem=controltype[0].toString();
}

Categories