Group contents in GridView - c#

I have some n no of rows in my GridView. Those items are categorized into some categories.
For example first 10 rows are categorized into a category and second 7 rows are categorized into second category.
There is a column named category in the Binding DataTable. Basing on that column the gridview has to be divided into categories.
Grouping in my sense is Background color of the category has to be changed for particular category.

You could create a CSS class for each of the categories that would set the background color that you want. Then set the CSS class for each data row in your GridView in the RowDataBound event.
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.CssClass = ((MyDataClass) e.Row.DataItem).Category;
}
}

Try using the templates of the ListView instead to avail of this grouping ability.
ListView Grouping by Data Field

Related

Link multiple Datagridviews in one form C#

I have created a form in c# which contains three datagridviews from three sql tables. One field is common for all gridviews. If i select a checkbox in a gridview automatically other gridview check box columns should be selected where the common field value is equal. First grid view contains itmcod, title, procod, typcod these four fields. second one contains title,procod,defalt and the third one contains title,typcod,defalt, procod, title2. The value procod is common for all three datable. I i select a row in gridview 2 automatically gridview 1 & 3 should select all values where procod value is equal. How can i do this
If you like only whole rows to be selected, then I suggest you to set .SelectionMode to FullRowSelect and MultiSelect to false, if you want to focus on only one row at a time. Then, you can achieve what you want like that in the SelectionChanged event of your first datagridview:
void DataGridView1SelectionChanged(object sender, EventArgs e)
{
//If MultiSelect = true
//foreach(DataGridViewRow row in dataGridView1.SelectedRows)
//If multiselect = false
using(DataGridViewRow row = dataGridView1.CurrentRow)
{
foreach(DataGridViewRow rs in dataGridView2.Rows.Cast<DataGridViewRow>().Concat(dataGridView3.Rows.Cast<DataGridViewRow>()).Where(r => !r.IsNewRow).Where(r => r.Cells["procod"].Value.ToString().Trim() != String.Empty).Where(r => r.Cells["procod"].Value.ToString() == row.Cells["procod"].Value.ToString()))
rs.Selected = true;
}
}
This just loops the rows in the other two datagridviews, that have the same value of the cell in the "procod" column.

c# Fill datagridviewcell from another datagridview

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;
}

How to populate a GridView drop-down list choices from SQL?

Here is what I want my Grid View to do.
My SQL table Pharmacy_Data contains Item_Name and Item_Code
Populate the Item_Code from Pharmacy_Data table. When I select the Item Name from the dropdown, It should fill the Item Code in the field.
How do I go about this?
1.Set Autopostback as true for Item name dropdownlist and write code for fetch data (Item code) inside the dropdownlist change event and fill Item code
Refer to the following Link.. This might help you in this. In the example in the link they use direct value to fill the dropdown using list instead use database for your case.
I resolved this issue by adding a OnRowDataBound function to my GridView.
OnRowDataBound="Grid_ItemList_RowDataBound"
protected void Grid_ItemList_RowDataBound(object sender, GridViewRowEventArgs e)
{
Connection con = new Connection();
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlnames = (e.Row.FindControl("ddlnames") as DropDownList);
ddlnames.DataSource = con.GetData("Select Item_Code,Item_Name from mytable");
//TextValue Pairs of DropDown list
ddlnames.DataTextField = "Item_Name";
ddlnames.DataValueField = "Item_Code";
ddlnames.DataBind();
//Add Default Item in the DropDownList
ddlnames.Items.Insert(0, new ListItem("Please select"));
}
}
The above code pulls the ItemNames and their respective ItemCodes.
Now, to display the codes in the textbox on selection, I used the below event for the dropdown element
OnSelectedIndexChanged="ddlnames_OnChanged"
protected void ddlnames_OnChanged(object sender, EventArgs e)
{
//Encapsulates the object back as a dropdown list
DropDownList ddlnames = (DropDownList)sender;
//Finds the control in the corresponding gridview row and edits the label
GridViewRow row = (GridViewRow)ddlnames.NamingContainer;
TextBox IC = (TextBox)row.FindControl("itemCode");
IC.Text = ddlnames.SelectedValue;
}
Well, that's what I had to do! :)
Hope someone finds this useful someday.

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.

moving columns in gridview between template fields and bound fields

I have a gridview (GridviewProduct) in that i Have a Template field column Link button as Remove From Cart and and another Template Field column next to it as Quantity and other three Fields are boundField like ProductName, ProductPrice and ProductBrand. Which Looks as Follows:
I want to move the quantity column to the end of the gridview at the right hand side. when i use this code it gives me an error:
Insertion index was out of range
var Quantity = GridViewProduct.Columns[1];
GridViewProduct.Columns.RemoveAt(1);
GridViewProduct.Columns.Insert(5, Quantity);
please help.
your requirement can be achieved in Gridview's RowCreated event
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
List<TableCell> columns = new List<TableCell>();
//Get the first Cell /Column
TableCell cell = row.Cells[1];
// Then Remove it after
row.Cells.Remove(cell);
//And Add it to the List Collections
columns.Add(cell);
// Add cells
row.Cells.AddRange(columns.ToArray());
}
If you have 5 columns and remove one of them, you have 4 left. The index of a new last column would than be 4 (since it is zero-based).
GridViewProduct.Columns.Insert(4, Quantity);
I am not sure if this removes all problems, but at least this error should go...

Categories

Resources