Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid

Status
Not open for further replies.

plsh

Programmer
Mar 5, 2003
118
ZA
Hi there,

I have a datagrid that I have bound to SAP using the .NET connector. I can fill the grid no problem, but I would now like to export the results of that datagrid to a text file where some criteria are met. Does anyone know how to read the contents of the datagrid cells? I am using c#.

Thanks in advance
 
how did you connect the data to the datagrid? did you use a dataadapter? if so, simply add a new step by also creating a dataset and filling the dataset with the dataadapter. then cycle through the dataset's newly created table or add criterias like row filter to get the rows you need.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Thanks DaZZled,

What ideally I would like to do is fill the datagrid with all the rows and then loop through the actual datagrid itself.
 
the datagrid has to have a datasource. you have to loop through the datasource to get the data from the datagrid. depending on the datasource this has several ways of doing the loop. that's why I suggested to use an itermediary step like a dataset that will not affect the current logic in any way.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
I must be honst in that I really do not know how to do that. I bind my datagrid to a SAP table Wizard, so I am not sure if or how to do what you are saying. That is why I would like to just loop through the datagrid and access the values in the cells directly. Something like:
datagrid1.cells(row,col).value

Something along those lines, but can't find anything remotely like this.
 
could you please post the code generated by the wizard for binding the data?

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
The data isretrieved from SAP with the SAP table wizard and then the datagrid is bound to this. The way I retrieve the data with this is as follows:

private void cmdRetrieve_Click(object sender, System.EventArgs e)
{
// Declare parameters here

TEST proxy = new TEST();
using(proxy.Connection = new SAP.Connector.SAPConnection(this.sapLogonDestination1))
{

// Call methods here
proxy.RFC_CALLED(ref this.TEST);

}
}

Most of the code is done within the SAP table wizard so not too much control.
 
hmmm... is there no help available for the SAP table wizard?

try setting a breakpoint after the datagrid is bound and then using the watcher see what type of object the datasource is and what properties it has. you might be able to cast the datasource to that type of object (probably something derived from DataTable and then loop through the records there)

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
There is very limited information that I can find out about the SAP .NET connector which is the main reason I am trying to loop through the grid and retrieve the values of the various cells.

Do you have any idea how this could possibly be done? I know it is not best coding or anything like that but right now if it works I really do not care.
 
you could use something like
Code:
foreach (DataGridItem dgRow in myDatagrid.Items)
{
     // call all the cells of the row using dgRow.Cells[cellIndex]
}

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
I get an error saying
'System.Windows.Forms.DataGrid' does not contain a definition for 'Items'

Any ideas?
 
yes... you are developping a windows app and windows forms' datagrid does not have Items.

set a breakpoint after the myDatagrid1.Datasource = ... and see what type of object is the DataSource now.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
ok... here's what I've come up with:
Code:
CurrencyManager cm = (CurrencyManager)this.BindingContext[this.dataGrid1.DataSource];

// iterate the rows
for (int i=0; i < cm.List.Count; i++)
{
    // iterate the cells
    DataRowView drv = (DataRowView)cm.List[i];
    for (int j=0; j < drv.Row.ItemArray.Length; j++)
    {
         // retieve the cell's value
         string cellValue = drv.Row.ItemArray[j].ToString();
    }
}

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Just what I was looking for, thank you sooooo much.

 
phew... you're welcome

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top