0

I'm having a problem when I want to make a table using a DataTable and a DataGrid, simply by saying the Rows' lines it appears a following error:

DataGrid does not contain a definition for' Rows' and could not find any 'Rows' extension method that accepts a first argument of type' DataGrid '(is there a usage directive or missing assembly reference?)

I really do not know what to do, read the code I used below, the part of the error I left in bold.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btn_salvar_Click(object sender, RoutedEventArgs e)
    {
        DataTable Tabela = new DataTable();

        Tabela.Columns.Add("Name");
        Tabela.Columns.Add("Region:");

        DataRow row = Tabela.NewRow();
        row["Name"] = "";
        row["Region"] = "";
        Tabela.Rows.Add(row);

        foreach (DataRow drow in Tabela.Rows) ;
        {
            int Livros = Tabela_.**ROWS**.Add();
            Tabela_Livro.**ROWS**[num].Cells[0]Value = drow["Name"].ToString();
            Tabela_Livro.**ROWS**[num].Cells[1]Value = drow["Region"].ToString();
        }

    }
}
T.S.
  • 18,195
  • 11
  • 58
  • 78
DMK
  • 1
  • 1
  • What's with the for..each? Your table only has the one row you added. – LarsTech Oct 20 '17 at 23:39
  • You have a semicolon at the end of the foreach line that will cause trouble. – DSway Oct 20 '17 at 23:51
  • 1
    What the hell is this - `Tabela_.**ROWS**.Add();`??? – T.S. Oct 21 '17 at 00:40
  • If have data table, no need for `for-loop`. Just assign datasource – T.S. Oct 21 '17 at 00:45
  • If you have a DataGridView then all you need to do to add DataTable is : datagridview1.DataSource = dt; – jdweng Oct 21 '17 at 02:46
  • I put this to make the error highlighted, I am new to the site. @jdweng I wanted to do it in a way that did not need servers (sql) so I chose the DataTable. – DMK Oct 21 '17 at 17:56
  • You are not correct. Using DataSource in this case has nothing to do with a server. A DataTable is an object in the System.Data library that is part of your application with no connections to a server. – jdweng Oct 21 '17 at 23:24
  • @jdweng how or where could I be implementing it in my code? because I tried to put it in the same place as the last and gave the same problem, only instead of 'Rows gave' DataSource ' – DMK Oct 22 '17 at 01:53
  • Remove the for loop and replace with : Tabela_Livro.DataSource = Tablea; – jdweng Oct 22 '17 at 06:40
  • @jdweng continues with the same error, only instead of 'Rows' happens 'DataSource' looks at the error: Error CS1061 "DataGrid" does not contain a definition for "DataSource" and could not find any "DataSource" extension method that accepts a first argument of type "DataGrid" (is there a usage directive or missing assembly reference?) – DMK Oct 22 '17 at 14:00
  • Are you using a DataGridView or something else? If it is a different control then you may have to use the binding property. – jdweng Oct 22 '17 at 15:04
  • @jdweng I am using a DataGrid which is the same thing as a DataGridView is not it? I did not find the one with the name 'View' added. – DMK Oct 22 '17 at 17:54
  • Absolutely NO!!! – jdweng Oct 22 '17 at 21:53
  • @jdweng could you tell me how I get access to a DataGridView? – DMK Oct 23 '17 at 00:12
  • DGV is only available on a Form. You have a wpf. So bind like solutions on following posting : https://stackoverflow.com/questions/5809816/datagrid-binding-in-wpf – jdweng Oct 23 '17 at 01:17
  • @jdweng I managed to solve my problem, I had created the project in WPF so I did not find the DataGridView now I created another project in Windows Form and implemented that code! Thank you for your help and patience. – DMK Oct 24 '17 at 15:30

0 Answers0