Problem with tableView: can not convert the type 'System.Data.DataTable' to 'AppKit.INSTableViewDataSource

2

When I try to show the data in my database in tableView I get that error and if I convert it in NSTableViewDataSource I get a System.ExceptionCatch . It's the first time this error has happened to me. If someone knows how to fix it or thinks it can be please tell me about it.

Sorry for not uploading it in text format and thanks for the suggestions. Here it is:

public partial class ViewController : NSViewController
{
    dataBase sql = new dataBase();



    public ViewController(IntPtr handle) : base(handle)
    {

    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        tableView.DataSource = sql.MostrarDatos();
         // Do any additional setup after loading the view.
    }

    public override NSObject RepresentedObject
    {
        get
        {
            return base.RepresentedObject;
        }
        set
        {
            base.RepresentedObject = value;
            // Update the view, if already loaded.
        }
    }



}

And this is the error:

  

Error CS0266: Can not implicitly convert type 'System.Data.DataTable' to 'AppKit.INSTableViewDataSource'. An explicit conversion exists (are you missing a cast?) (CS0266)

And here the ShowData code:

 public class dataBase
{



    private MySqlConnection cnx = new MySqlConnection("server = 127.0.0.1; database=Facturas; Uid=root;pwd=pablo_37;");
    private DataSet ds;

    public void ProbarConexion()
    {

        var alert = new NSAlert();
        try
        {

            cnx.Open();
            alert.InformativeText = "Conectado";
            alert.RunModal();


        }
        catch (Exception r)
        {

            alert.MessageText = r.Message;
            alert.RunModal();
        }
        finally { cnx.Close(); }



    }

    public DataTable MostrarDatos()
    {


        cnx.Open();
        MySqlCommand cmd = new MySqlCommand("select * from Factura", cnx);
        MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
        ds = new DataSet();
        ad.Fill(ds, "Factura");
        return ds.Tables["Factura"];


    }

 }

For the application I am using visual studio with Xcode and mysql in case someone has already worked with it and knows something about the problem or another solution to be able to query the tables through them. The application is desktop. Thanks for your help and comments in advance.

    
asked by Pablo 25.04.2018 в 12:49
source

1 answer

0

The view should be UIViewController, not NSViewController The datasource of the table must be in lowercase. You have to assign a something that complies with the datasource protocol of the TableView, it does not work with you plugging in the database connection and you have to know everything. So:

tableView.datasource = sql

and in the database class you have to implement it in table datasource with its methods:

extension database: UITableViewDataSource {
//metodos del UITableViewDataSource
   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       //te tendrás que buscarte la vida para saber que celda devuelves

   }

  func numberOfSections(in tableView: UITableView) -> Int {
     //tendras que buscar en la BD cuantas secciones tienes y devolverlo
  }

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      //tendras que buscar en la BD cuantas celdas hay en cada sección
  }
    
answered by 26.04.2018 в 12:25