kiran kumar

kiran kumar

  • NA
  • 3
  • 3.3k

DataGridView in UserControl is not displaying any values

Feb 24 2012 8:06 AM
Hi all,

I have a very strange problem here, Please help me

My windows application consists of a Form in which I placed a TabControl with 3 Tab Pages.

In second TabPage i place a SplitContainer, and in Panel1 of SplitContainer i placed a ToolStrip with two Buttons, one of which is "Change Plans" Button.

And in second Panel of SplitContainer i am going to add a UserControl called "userControlChangePlans.cs" at runtime, when I click "Change Plans" Button which is present in ToolStrip i added to Panel1 of the SplitContainer.

Overall, my Form looks like this,



Now, the problem is,


I have a DataGridView in the UserControl "userControlChangePlans.cs" which I mentioned above.

So, when I click "Change Plans" button which is present in ToolStrip i added to Panel1 of the SplitContainer, the UserControl "userControlChangePlans.cs" which consist of DataGridView with values has to display in Panel2 of SplitContainer.

 

But this is not happening; my DataGridView is not showing any values.

 

Eventhough my UserControl is showing in the Panel2 of SplitContainer, my DataGridView is not showing any values.


Please, can anybody help?

I am Writing the code below :

  1. DataAcess class, Class which contains all methods to retrieving data from databases.

 

public class DataAcess

{

SqlConnection conObj = new  SqlConnection(ConfigurationSettings.AppSettings["ConStringDB"]);   

  SqlCommand cmdObj;

  SqlDataAdapter daObj;

DataSet dsObj;

 

public DataSet GetExistedData(string TblName, string Fields, string WhereStr)

    {

        dsObj = new DataSet();

        string QueryStr = "";

        try

        {

            if (WhereStr != "")

                QueryStr = "select " + Fields + " from " + TblName + " where " + WhereStr;

            else

                QueryStr = "select " + Fields + " from " + TblName;

            daObj = new SqlDataAdapter(QueryStr, conObj);

            daObj.Fill(dsObj, "Tbl_CropDetails");

 

        }

        catch (Exception ex)

        {

          

        }

        finally

        {

 

        }

        return dsObj;

    }

 

2.   UserControl "userControlChangePlans.cs"

 

 

namespace WindowsFormsApplication1

{

   

    public partial class userControlChangePlans : UserControl

    {

      

        public userControlChangePlans()

        {

            InitializeComponent();

        }

 

       

        public object DataSourceforDataGrid

        {

 

            get { return dataGridViewPlans.DataSource; }

            set { dataGridViewPlans.DataSource = value; }

        }

 

    }

}



ToolSrip Button "Change Plans" Click Event

 

DataAcess objDA = new DataAcess();

DataSet objDs = null;

 

private void tsbtnChangePlans_Click(object sender, EventArgs e)

        {

                        tsbtnChangePlans.BackColor = SystemColors.GradientActiveCaption;

            tsbtnChangePlans.Font = new Font(tsbtnChangePlans.Font, FontStyle.Underline);

            splitContainer1.Panel2.Controls.Clear();

            userControlChangePlans usrcntrlChngPlan = new userControlChangePlans();           

            objDs = objDA.GetExistedData("tblplan", "*", "");

            usrcntrlChngPlan.DataSourceforDataGrid = objDs;

            splitContainer1.Panel2.Controls.Add(usrcntrlChngPlan);

            //MessageBox.Show(objDs.Tables[0].Rows[0][0].ToString());

        }

 

In the last line which I commented out, I am just checking whether my Dataset contains any value,

Its working, MessageBox is showing the values of the Row[0][0],

But the same Dataset which I added as DataSource to DataGridView is not showing the values.

Please help me with this issue, i want to know where i am wrong.

Thanks