dataGridView help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?R2lkaQ==?=

    #1

    dataGridView help

    Hi,

    I have a dataGridView in my win Form, and i get some information from my
    DataBase (sql) into a dataTable.

    I tried and tried and i can't display my result in the dataGridView.

    in addition, I also want to edit the columns (change the headr and size), i
    know i can do it with the columns collection, but i need to bind each column
    to a coulmn in my dataBase, and i don't know how to do it.

    this is my code:

    com = new SqlCommand();
    da = new SqlDataAdapter( );
    // dt = new DataTable();

    //
    // com
    //

    this.com.Comman dText = "[Get_Message_By_ Person]";
    this.com.Comman dType = System.Data.Com mandType.Stored Procedure;
    this.com.Connec tion = m_mc.Get_dbcon( ).GetConn();
    this.com.Parame ters.Add(new
    System.Data.Sql Client.SqlParam eter("@RETURN_V ALUE",
    System.Data.Sql DbType.Int, 4, System.Data.Par ameterDirection .ReturnValue,
    false, ((System.Byte)( 10)), ((System.Byte)( 0)), "",
    System.Data.Dat aRowVersion.Cur rent, null));

    //
    // da
    //
    this.da.SelectC ommand = this.com;

    System.Data.Sql Client.SqlParam eter msg_to =
    this.com.Parame ters.Add(new System.Data.Sql Client.SqlParam eter("@msg_to",
    System.Data.Sql DbType.VarChar, 150));
    msg_to.Value = employees_cb.Te xt;

    da.Fill(msg_dt) ;

    msg_dgv.DataSou rce = msg_dt;

    thanks,
    Gidi.
  • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

    #2
    RE: dataGridView help

    The first thing I notice about your sample is that nowhere is the msg_dt
    Datatable created before the Fill method is called. You need to have
    DataTable msg_dt = new DataTable();
    somewhere before you pass it into the Fill method. Some exception handling
    would have also been helpful, as you would have seen the exception if you
    caught it.
    --Peter
    Recursion: see Recursion
    site: http://www.eggheadcafe.com
    unBlog: http://petesbloggerama.blogspot.com
    BlogMetaFinder: http://www.blogmetafinder.com



    "Gidi" wrote:
    Hi,
    >
    I have a dataGridView in my win Form, and i get some information from my
    DataBase (sql) into a dataTable.
    >
    I tried and tried and i can't display my result in the dataGridView.
    >
    in addition, I also want to edit the columns (change the headr and size), i
    know i can do it with the columns collection, but i need to bind each column
    to a coulmn in my dataBase, and i don't know how to do it.
    >
    this is my code:
    >
    com = new SqlCommand();
    da = new SqlDataAdapter( );
    // dt = new DataTable();
    >
    //
    // com
    //
    >
    this.com.Comman dText = "[Get_Message_By_ Person]";
    this.com.Comman dType = System.Data.Com mandType.Stored Procedure;
    this.com.Connec tion = m_mc.Get_dbcon( ).GetConn();
    this.com.Parame ters.Add(new
    System.Data.Sql Client.SqlParam eter("@RETURN_V ALUE",
    System.Data.Sql DbType.Int, 4, System.Data.Par ameterDirection .ReturnValue,
    false, ((System.Byte)( 10)), ((System.Byte)( 0)), "",
    System.Data.Dat aRowVersion.Cur rent, null));
    >
    //
    // da
    //
    this.da.SelectC ommand = this.com;
    >
    System.Data.Sql Client.SqlParam eter msg_to =
    this.com.Parame ters.Add(new System.Data.Sql Client.SqlParam eter("@msg_to",
    System.Data.Sql DbType.VarChar, 150));
    msg_to.Value = employees_cb.Te xt;
    >
    da.Fill(msg_dt) ;
    >
    msg_dgv.DataSou rce = msg_dt;
    >
    thanks,
    Gidi.

    Comment

    • =?Utf-8?B?R2lkaQ==?=

      #3
      RE: dataGridView help

      Hi,

      I've the DataTable msg_dt = new DataTable(); i just put it in the Initialze
      part and not in this part of code, so i still have these 2 problems.

      Thanks,
      Gidi.

      "Peter Bromberg [C# MVP]" wrote:
      The first thing I notice about your sample is that nowhere is the msg_dt
      Datatable created before the Fill method is called. You need to have
      DataTable msg_dt = new DataTable();
      somewhere before you pass it into the Fill method. Some exception handling
      would have also been helpful, as you would have seen the exception if you
      caught it.
      --Peter
      Recursion: see Recursion
      site: http://www.eggheadcafe.com
      unBlog: http://petesbloggerama.blogspot.com
      BlogMetaFinder: http://www.blogmetafinder.com
      >
      >
      >
      "Gidi" wrote:
      >
      Hi,

      I have a dataGridView in my win Form, and i get some information from my
      DataBase (sql) into a dataTable.

      I tried and tried and i can't display my result in the dataGridView.

      in addition, I also want to edit the columns (change the headr and size), i
      know i can do it with the columns collection, but i need to bind each column
      to a coulmn in my dataBase, and i don't know how to do it.

      this is my code:

      com = new SqlCommand();
      da = new SqlDataAdapter( );
      // dt = new DataTable();

      //
      // com
      //

      this.com.Comman dText = "[Get_Message_By_ Person]";
      this.com.Comman dType = System.Data.Com mandType.Stored Procedure;
      this.com.Connec tion = m_mc.Get_dbcon( ).GetConn();
      this.com.Parame ters.Add(new
      System.Data.Sql Client.SqlParam eter("@RETURN_V ALUE",
      System.Data.Sql DbType.Int, 4, System.Data.Par ameterDirection .ReturnValue,
      false, ((System.Byte)( 10)), ((System.Byte)( 0)), "",
      System.Data.Dat aRowVersion.Cur rent, null));

      //
      // da
      //
      this.da.SelectC ommand = this.com;

      System.Data.Sql Client.SqlParam eter msg_to =
      this.com.Parame ters.Add(new System.Data.Sql Client.SqlParam eter("@msg_to",
      System.Data.Sql DbType.VarChar, 150));
      msg_to.Value = employees_cb.Te xt;

      da.Fill(msg_dt) ;

      msg_dgv.DataSou rce = msg_dt;

      thanks,
      Gidi.

      Comment

      • bob clegg

        #4
        Re: dataGridView help


        Hi Gidi,
        I usually declare a dataset which houses a data table
        that has field names and types the same as the selected fields in the
        sproc.
        I ask the dataadapter to fill the dataset's table then
        pass the dataset back to the datagridview.
        i.e.
        void LoadData
        {
        myTypedDataset ds = myDataFillingFu nction();
        myDataGridView. DataSource = ds;
        myDataGridview. DataMember = ds.myDataTable. TableName;
        }

        myTypedDataset myDataFillingFu nction()
        {
        Connection stuff and parameter stuff
        myTypedDataset d = new myTypedDataSet( );
        da.Fill(d.myDat aTable);//intellsense will show you the table
        return d;
        }

        As Peter said exception handling will allow you to see if the SPROC is
        happy with your handiwork.

        hth
        Bob

        On Fri, 12 Oct 2007 14:40:01 -0700, Gidi
        <shnapsi@hotmai l.com.dontspamw rote:
        >Hi,
        >
        >I've the DataTable msg_dt = new DataTable(); i just put it in the Initialze
        >part and not in this part of code, so i still have these 2 problems.
        >
        >Thanks,
        >Gidi.
        >
        >"Peter Bromberg [C# MVP]" wrote:
        >
        >The first thing I notice about your sample is that nowhere is the msg_dt
        >Datatable created before the Fill method is called. You need to have
        >DataTable msg_dt = new DataTable();
        >somewhere before you pass it into the Fill method. Some exception handling
        >would have also been helpful, as you would have seen the exception if you
        >caught it.
        >--Peter
        >Recursion: see Recursion
        >site: http://www.eggheadcafe.com
        >unBlog: http://petesbloggerama.blogspot.com
        >BlogMetaFinder : http://www.blogmetafinder.com
        >>
        >>
        >>
        >"Gidi" wrote:
        >>
        Hi,
        >
        I have a dataGridView in my win Form, and i get some information from my
        DataBase (sql) into a dataTable.
        >
        I tried and tried and i can't display my result in the dataGridView.
        >
        in addition, I also want to edit the columns (change the headr and size), i
        know i can do it with the columns collection, but i need to bind each column
        to a coulmn in my dataBase, and i don't know how to do it.
        >
        this is my code:
        >
        com = new SqlCommand();
        da = new SqlDataAdapter( );
        // dt = new DataTable();
        >
        //
        // com
        //
        >
        this.com.Comman dText = "[Get_Message_By_ Person]";
        this.com.Comman dType = System.Data.Com mandType.Stored Procedure;
        this.com.Connec tion = m_mc.Get_dbcon( ).GetConn();
        this.com.Parame ters.Add(new
        System.Data.Sql Client.SqlParam eter("@RETURN_V ALUE",
        System.Data.Sql DbType.Int, 4, System.Data.Par ameterDirection .ReturnValue,
        false, ((System.Byte)( 10)), ((System.Byte)( 0)), "",
        System.Data.Dat aRowVersion.Cur rent, null));
        >
        //
        // da
        //
        this.da.SelectC ommand = this.com;
        >
        System.Data.Sql Client.SqlParam eter msg_to =
        this.com.Parame ters.Add(new System.Data.Sql Client.SqlParam eter("@msg_to",
        System.Data.Sql DbType.VarChar, 150));
        msg_to.Value = employees_cb.Te xt;
        >
        da.Fill(msg_dt) ;
        >
        msg_dgv.DataSou rce = msg_dt;
        >
        thanks,
        Gidi.

        Comment

        Working...