c# Fill datagridviewcell from another datagridview - c#

I have two datagridviews in my code.
One of them is the maingrid, which is connected to a database.
The second one is only a list with one column.
I wrote an event for doubleclicking a cell in the second grid
private void xmlGrid_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
string cellContent = xmlGrid.CurrentCell.Value.ToString();
MessageBox.Show(cellContent);
}
The first grid has 4 columns "id|ChannelNumber|ChannelName|XMLChannelName"
The first 3 Columns are filled from the database. The 4. column should be filled by the value of the doubleclick event with the text.
This Event should fill the XMLChannelname Cell of the selected row in the first grid with the value text from the event.

You need to loop over rows in first grid (say Grid1) and figure out which row will get the value from second grid (say Grid2). Lets assume that ChannelName column is used to decide which row will get value. Then following will work:
private void xmlGrid_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
string cellContent = xmlGrid.CurrentCell.Value.ToString();
if(Grid1.SelectedRows.Count==0) return;
var row = Grid1.SelectedRows[0];
row.Cells[3].Value=cellContent;
}

Related

Is it possible to disable alternating rows for a single column in a DataGridView?

I'm trying to add a column to an already populated DataGridView but I need this column cells to be all the same color since it is the sum of all the columns on each row.
I add the column this way:
dt.Columns.Add("Acumulable", typeof(float));
dt.Columns["Acumulable"].Expression = "Enero+Febrero+Marzo+Abril+Mayo+Junio+Julio+Agosto+Septiembre+Octubre+Noviembre+Diciembre";
And I set the color like this:
DGVGastosVariables.Columns["Acumulable"].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#3498DB");
It works fine since it does change the color of the cells but it keeps the alternating row pattern.
Is there a way to disable the alternating rows for that column in particular?
Thanks.
Try this. Subscribe your DataGridView to CellFormatting event:
private void DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == /* Acumulable column index */)
{
e.CellStyle.BackColor = ColorTranslator.FromHtml("#3498DB");
}
}
Also you can use CellPainting event.

How to get selected full cell values and compare with database row cell values and update the particular row with new value?

I have a gridview which is databound and lets say that i have got around 20 rows in gridview while running. The gridview also has checkboxes for each rows and if i click only particular rows using checkbox the following operation should be performed using c# asp.net and linq.
The fields in gridview are name,area,problem,status,date and time.if i check the checkbox for some rows, the values (name,date and time) of selected rows should be matched with the values in database and in database the status column should be updated with 'completed' value and the remaining unselected rows should be matched similarly with database values and should update the status as 'pending'...
Please help me solve this situation..
Events CellBeginEdit and CellEndEdit
Usage:
public object CurrentCellValue;
private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
CurrentCellValue = dataGridView[e.ColumnIndex, e.RowIndex].Value;
}
private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
var cellVaue = dataGridView[e.ColumnIndex, e.RowIndex].Value;
if (!ValidateTime(cellVaue.ToString()))
dataGridView[e.ColumnIndex, e.RowIndex].Value = CurrentCellValue;
}

Datagridview Importing Old Row Data when Row Changed

I have two DataGridViews, A and B, and one DataSet "D" with two tables "tA" and "tB" identical in structure. Each DataGridView is connected to its respective table by its own BindingSource.
One of the columns, named "Complete", contains checkboxes to mark a data row as "Complete". When such a checkbox in DataGridView A is checked by the user, I want to immediately move that datarow from dgv A to dgv B. I've accomplished this using the eventhandling code below
void datagridviewA_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (datagridviewA.IsCurrentCellDirty)
{
datagridviewA.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
void datagridviewA_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 11) // 11 is the column index containing the "complete" checkboxes
{
tB.ImportRow(tA.Rows[e.RowIndex]);
tA.Rows.RemoveAt(e.RowIndex);
}
}
This works to move the row from dgv A to dgv B, but I expected the "complete" checkbox cell to be checked in dgv B, but it's not. It's as if the row is being imported from tA before tA is updated to reflect the "checked" status of the checkbox.
How can I move the row and keep the checkbox checked as expected?
Thanks,
Try to cast the cell to DataGridViewCheckBoxCell and set Checked property to true.
I had to call "tA.Rows[e.RowIndex].AcceptChanges();" before importing the row!

How do I extract specific values from SELECTED/HIGHLIGHTED rows on a GridVIew in ASP .NET?

I have a GridView with data. The first column on this GridView is the "SELECT" column.
If the user clicks on SELECT, it highlights the entire row.
I have an event on this click action:
protected void gvShows_SelectedIndexChanged(object sender, EventArgs e)
{
}
Basically what I want to do is for each SELECTED row, I want to extract the values for the columns:
dataSource
showId
episodeId
**Here's where I'm having problems:
string dataSource = "";
int showId = 0;
int episodeId = 0;
foreach (DataRow row in gvShows.Rows)
if (the row is selected/highlighted then...)
{
dataSource = the value under column "dataSrouce" for this ROW.
showId = the value under column "showId" for this ROW.
episodeId = the value under column "episodeId" for this ROW.
}
Can anyone give me a hand with this?
I believe you will want to use SelectedIndexChanging (and not SelectedIndexChanged) because that will give you access to the new selected row index. If the first cell is the button, the next three should give you the values you need:
protected void gvShows_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow r = gvShows.Rows[e.NewSelectedIndex];
string dataSource = r.Cells[1].Text;
int showId = Convert.ToInt32(r.Cells[2].Text);
int episodeId = Convert.ToInt32(r.Cells[3].Text);
}
Edit:
I wanted to add that you can use the SelectedIndexChanged if you didn't want to use SelectedIndexChanging:
protected void gvShows_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow r = gvShows.Rows[gvShows.SelectedIndex];
string dataSource = r.Cells[1].Text;
int showId = Convert.ToInt32(r.Cells[2].Text);
int episodeId = Convert.ToInt32(r.Cells[3].Text);
}
And if you are working with a database, you can add a datakey to the gridview, allowing you to pull the primary key for the record you want to work on.
Add the following to your gridview:
DataKeyNames="showId"
And then you can access this value from the codebehind:
int showId = Convert.ToInt32(gvShows.DataKeys[gvShows.SelectedIndex].Value);
You need to rely on the "selected/highlighted" rows' attributes. Find any attribute that could determine if it is a selected row.
EventArgs e should have a Row property that represents the selected row. Within there, you have access to each of the columns (called Cells). You get into each particular cell using indexing. So, if ShowId is in column 3, here's how you'd access it:
e.Row.Cells[2]; (remember, it's 0-based indexing in C#).
First to get ShowId, try e.Row.Cells[2].Text; That might get you what you need.
If that doesn't work, try this:
Now, within Cells[2], you have access to Controls, which again is a list of controls in that cell. Run your code, and put a breakpoint inside of SelectedIndexChanged. Then in your immediate window, type the code above, and hover your mouse over Cells[2] to open up the items in there. Hover again on Controls, and look at what's in there.
Usually there's a LiteralControl, then your control with ShowId, then another LiteralControl.
Validate that, and then you'll be able to access it like e.Row.Cells[2].Controls[1].Text or something like that.

Get specific cell of a selected DataGridViewRow in editmode independent of the cell, which is entered for selection

I have a DataGridView, which has two columns and several rows. The first column has the names of some attributes of an object and the second one should be filled with values by the user.
How can I get the second column of a selected row automatically in editmode independent of the column, which is entered to select the row?
This means, if a row is selected it should be immediately possible to enter some values into the second column of that specific row, even if the row is selected by entering the first column of the row.
By now, the first column has the property DataGridRowColumns.ReadOnly = true and the second one has DataGridViewColumn.ReadOnly = false. The grid has the following properties: DataGridView.MultiSelect = false and DataGridView.SelectionMode = FullRowSelect.
You can try with this code
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DataRowView dataItem1 = (DataRowView)e.Item.DataItem;
var result = (string)dataItem1.Row["YourColumnName"];
......
} }

Categories

Resources