C# Get Rows From DataGridView - c#

I need to get all the rows in a DataGridView in a foreach function. How can I do this?
I.E. foreach() for each row, so for each row I could run code that would utilize the first and second column data.
This is in c#
Thanks,
Christian

I think the best way of accessing this data is either through the Data Source:
dataGridView.DataSource = someData;
someData.property;
OR, if the user is entering data on the page, you can access from the FindControl method:
name = ((TextBox)dataGridView.Rows[e.RowIndex].FindControl("name")).Text;
In this case, if you've raised an event for a specific row, it will return EventArgs e, with a specific RowIndex. Then you can access the Column values via the ControlID within the column, such as <asp:TextBox id="name" runat="server" /> from .FindControl("name").
The important thing to remember is that you have to cast that object back to the type that it should be from the .FindControl() method.

Remember, it's always a good practice to bind the DataGridView to a data source, and then using the data source to do anything data-related. This keeps you clean from interacting with the datagrid.

foreach(DataGridViewRow row in dataGridView.Rows)
{
//Your code here
}

Related

Load checked datagridview row to another datagridview in another form

I have a DataGridView1 with data and a checkboxcolumn in the [0]
position.
In the form that contains the DataGridView1 with the checkboxes, I
have a button
Now, what I need is a way that allows me, after I check the rows I
want, get the checked rows to another form creating a small
datagridview with these rows.
I want to associate this function to the button_click event.
It's all about multiple selection, the basic "import the lines you want to a previous form".
Can anyone help me?
BIG THANK YOU
foreach (DataGridViewRow row in YourDgvMain.Rows)
{
if ((bool)row.Cells[0].Value == true)
{
YourOtherDgv.Rows.add(row);
}
}
My solution is a bit messy but should work: bind the row id to the checkbox, store the checked values in a collection, query again the datasource with the condition WHERE [id] IN ([your collection]) and create the second datagridview with the result of the query.
You should specify however what is the source for the gridview and how do you retrieve the data to get a better answer.

Accessing another row while in Gridview RowDataBound?

Is there a way to access elements that are in another row, while you're in a RowDataBound event?
public void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// how to compare e.Row with row above or row below?
}
One thing I think you can do is...
foreach (GridViewRow gvrow in GridView1)
{
//loop through gridview's rows and find the row you're looking for
}
I believe that RowDataBound adds the rows in the same order that the data source has them in, therefore if there are 100 records in your DataTable and you bind that DataTable to the GridView, RowDataBound gets called 100 times, for each row, in the order they exist in the DataTable. Therefore, you mentioned using RowDataBound to compare e.Row with the row above OR row below...but if they are coming in sequentially, there is no row below. This is something I hadn't really thought about before but I recall when doing testing with breakpoints that RowDataBound functions in this manner.
If you can't get something like that working, and you need this to happen even if it isn't pretty and don't get any better answers, you can store a copy of your databound DataTable or whatever your source for data is into a ViewState or Session variable, like ViewState["myDataTable"]. You can then retrieve the DataTable on RowDataBound event, and look at the rows above and below the row that is represented by e.Row (provided that you have some kind of cursor like an ID to identify the rows). By doing this, you CAN look at the next row that will be added to the GridView by RowDataBound, because it will be the next row in the DataTable (which already exists and can be viewed).
If you're doing stuff on a massive scale though, I imagine all this looping could get cumbersome.
From a foggy memory, you can reference GridView's array of Rows from there and iterate them. You should be able to get e's position and access in any case the rows above, not sure about rows below as they aren't databound by then yet I suppose, unless this is a one-row rebind shot.

Get ObjectDataSource records in code-behind

I have a ObjectDataSource which is placed in de source code of my .aspx page, not the code behind, and is used inside an EditTemplate column of a datagrid.
<asp:DropDownList ID="ddlist1" runat="server" DataSourceID="osCreditType" ...
After selecting a value in the dropdown and setting the datagrid state back to ItemTemplate, I have the ValueMember of that DropDownItem in the NewValues collection inside the RowUpdating Event.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int primary = int.Parse(Convert.ToString(e.NewValues[0]));
}
Now, I want to retrieve some other information from the ObjectDataSource using that primary.
Is the data, used for the dropdown still available in the ObjectDataSource, or will a call to that source from code-behind make the datasource go back to the database
Can I use the ObjectDataSource to retrieve additional information using this primary key, and if so, how do I accomplish it ?
Thanks a lot in advance
You have to remember that the ObjectDataSource is really
just a Binder between your Data Layer and Your Controls.
What would be better is for you in the GridView1_RowUpdating routine
is to use a SqlDataAdapter and go and get the information from the database yourself
and then use that information to change the values of the Updated Row.
You will have a problem trying to run another Query with the Same ObjectDataSource,
cause as soon as you do that the control will try to rebind to the new data.
Hope this helps.

Change the value of every cell in a DataTable column

I have a System.Data.DataTable which I'm binding to a GridView. If the user doesn't have a certain security role, I want to replace the data in certain columns with an "X". Can I do this without looping through all of the rows?
Implement the GridView's RowDataBound event and make the change in there if the user's security role is inadequate.
void CusomGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(User.Role != "Admin")
{
e.Row.Cells[1].Text = "X";
}
}
Strictly speaking, no. You can use Linq-style extension methods to hide the implementation, but any code in a procedural language is going to involve an iterative loop through the rows. Even the RowDataBound event is fired iteratively by each row; however, it's iterating through the rows anyway, so at least you're not duplicating the looping behavior.
You could also do it on the back end. Pass the userID or role to an sp which returns only viewable columns and 'X' values for columns they can't see and bind the UI to that.
You could simply have a second column available and ready name it "your column2"
filled with x data, you then determine which column is visible based on admin access level.
In VB.NET you cannot change values of each row without recursion.

asp.net finding column from dynamic gridview

I'm creating a dynamic GridView from a DataTable that is returned from a stored procedure. I call and bind with the following code:
DataTable dt = Sql.reportData(Convert.ToInt32(Session["userID"]));
this.GridView1.DataSource = dt.DefaultView;
this.GridView1.DataBind();
I need to restyle certain columns but they are not always the same column number, and only have the headers text string to identify it. Is there an easy way to track a column down like this so I can edit its attributes?
Thanks,
Alex
I've run into this myself. You've got to loop through the column names, get the index, and then refer to the index to manipulate the style.
Muhammad is right about the timing, but you won't be searching for a label--it seems you want to style the entire column, right?
http://forums.asp.net/p/1076872/1584635.aspx
the above has several versions of a solution.
The best place to find the control and use it will be in the RowCreated event. RowDataBound should not be used because you dont have to manipulate the data with which the column is being binded. So restyle the elements in the column by searching them in the RowCreated event.
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.FindControl("");
}

Categories

Resources