C# .NET - datagridview-top urgent

Asked By thushara
15-Jun-11 12:09 PM
Is there any way to bind two tables in to datagridview.In my datagrid i want fields from two different tables having one field(empcode) in common
  Ravi S replied to thushara
15-Jun-11 12:16 PM
HI

Unforunately the datagridview only supports binding to one table at a time.  The old datagrid is still available for use you would just have to add it to your toolbox.  

create a treegridview column for the datagridview that might help

http://blogs.msdn.com/markrideout/archive/2006/01/08/customizing-the-datagridview-to-support-expanding-collapsing-ala-treegridview.aspx
  dipa ahuja replied to thushara
15-Jun-11 12:22 PM
You can do so with the join query by providing the join condition on common column between tables:

private void DataGrid_Load(object sender, EventArgs e)
{
  string query = "select e.empid,e.sal,	e.Address,ed.ename from edetail e inner join emp ed on e.empid=ed.empid";
  SqlDataAdapter da = new SqlDataAdapter(q, "ConnectionString");
 
  DataTable dt = new DataTable();
  da.Fill(dt);
 
  dataGridView1.DataBindings.Clear();
 
  dataGridView1.DataSource = dt;
      
}
  pete rainbow replied to thushara
15-Jun-11 12:32 PM
i've seen on the web this technique, although have to admit i've not used it...but it asically created a joined table in a dataset and popultaes it from the 2 distinct tables, they do have to be in the same dataset though...

googling some of these words getparentrow might help too...

DataRelation drel = new DataRelation("EquiJoin", dt2.Columns["KeyColumn"], dt1.Columns["KeyColumn"]);
ds.Relations.Add(drel);

DataTable jt = new DataTable("Joinedtable");

// now add all the columns of the 2 tables
jt.Columns.Add("Acol", typeof(Int32));
...

ds.Tables.Add(jt);

foreach (DataRow dr in ds.Tables["Table1"].Rows)
{
     DataRow parent = dr.GetParentRow("EquiJoin");
     DataRow current = jt.NewRow();

     // Just add all the columns' data in "dr" to the New table.
     for (int i = 0; i < ds.Tables["Table1"].Columns.Count; i++)
     {
       current[ i ] = dr[ i ];
     }

     // Add any columns that is not present in the child, which is present in the parent.
     current["other tab col"] = parent["other tab col"];

     jt.Rows.Add(current);

}

dataGridView1.DataSource = ds.Tables["Joinedtable"];
  Rohan Dave replied to thushara
15-Jun-11 02:52 PM
hey it's very simple.. you just need to use join to use two tables and pull and show the data in gridview... your code looks like below ...


string strQuery = "Select tblemp.EmpCode, tblemp.empname, tblED.Address1, tblED. address2, tblED.Country From EmployeeMaster tblemp INNER JOIN EmployeeDetail tblED on tnlemp.EmpCode = tblED.EmpCode ";

objAdpt = new SqlDataAdapter(strQuery, objCon);
objdt = new DataTable();
objAdpt.Fill(objdt);
gridview1.DataSource = objdt;
gridview1.DataBind();

that's it...
  Rohan Dave replied to thushara
15-Jun-11 02:52 PM
hey it's very simple.. you just need to use join to use two tables and pull and show the data in gridview... your code looks like below ...


string strQuery = "Select tblemp.EmpCode, tblemp.empname, tblED.Address1, tblED. address2, tblED.Country From EmployeeMaster tblemp INNER JOIN EmployeeDetail tblED on tnlemp.EmpCode = tblED.EmpCode ";

objAdpt = new SqlDataAdapter(strQuery, objCon);
objdt = new DataTable();
objAdpt.Fill(objdt);
gridview1.DataSource = objdt;
gridview1.DataBind();

that's it...
  [ Kirtan ] replied to thushara
15-Jun-11 04:42 PM
Untitled document Here is how to show data from two tables

public static void FillData()
{
   SqlConnection con = new SqlConnection("Connection String");
   con.Open();
   SqlCommand comm = new SqlCommand("select * from Table1 t1 INNER JOIN Table2 t2 ON t1.empid=t2.empid", con);
   SqlDataAdapter da = new SqlDataAdapter(comm);
   DataTable dt = new DataTable();
   da.Fill(dt);
   con.Close();
   dataGridView1.DataSource = dt;
}
  James H replied to thushara
15-Jun-11 11:58 PM
Google and read on what is Static & Dynamic Polymorphism.
OR
Google and read on what is Compile time & Runtime Polymorphism.
 
Further, as such it's not ASP.NET related, its OOP's related and thus you follow it while writing/designing code in a given language.

When inheriting from another class, you may wish to change the default bahaviour of a method or create a different method signature. You can do this by overloading the method with your own code.

Method signatures are formed from the parameter list, in particular the data types. Methods can share the same name within the class as long as the signature is different.

static int Add(int a, int b)
{
  return a + b;
}

In the above example the signature should be (int, int). In the Visual Studio code editor, if you were to call the Add method, after you enter the '(' Intellisense will popup the available parameters. Notice that there is only one option given to you.

We can add an overloaded method with a different signature and IntilliSense will now present two options, one for each signature of the overloaded method. The compiler finds two methods of the same name and two calls to that method with different parameters. It is able to tell the difference by comparing the signatures of the methods.

static int Add(int a, int b, int c)
{
  return a + b + c;
}

The names of the parameter and the return type have no effect on the method signature.

static decimal Add(int a, int b, int c)
{
  return a + b + c;
}

The above method will raise an error because a method already exists for Add(int, int), even though it returns a decimal.

Methods should be overloaded when you have similar methods that require differing parameters or you want to add new functionality to existing code, however you should not use overloading to often as it causes headaches during debugging and testing and is more effort to maintain.

Optional Parameters in C#

C# does not have an implementation of optional parameters like those found in php, however you can simulate this feature using method overloading.

static int Add(int a, int b)
{
  return Add(a, b, 0, 0);
}
 
static int Add(int a, int b, int c)
{
  return Add(a, b, c, 0);
}
 
static int Add(int a, int b, int c, int d)
{
  return (a + b + c + d);
}

In this example we can call Add with 2, 3 or 4 parameters and only one functional method is called. The others just pass the data around.

Override

Methods can be overridden, replacing the default behaviour or extending it. In this example we will override the ToString method for the ArrayList class.

using System;
using System.Collections.Generic;
 
class Program
{
  static void Main()
  {
     ArrayList myList = new ArrayList();
 
    myList.Add("Hello");
    myList.Add("World");
    myList.Add("123456");
 
    Console.WriteLine(myList.ToString());
  }
}

You may expect the ToString to return "Hello World 123456″, but it actually returns "System.Collections.ArrayList", which isn't particularly helpful.

We can override this default behaviour by creating a new class, which inherits from the ArrayList.

using System;
using System.Collections.Generic;
 
class Program
{
  static void Main()
  {
     myArrayList myList = new myArrayList();
 
    myList.Add("Hello");
    myList.Add("World");
    myList.Add("123456");
 
    Console.WriteLine(myList.ToString());
  }
}
 
class myArrayList : System.Collections.ArrayList
{
  public override string ToString()
  {
    string result = "";
    string[] theItems = (string[])base.ToArray(typeof(string));
 
    foreach (string item in theItems)
    {
      result += " " + item;
    }
    return result;
  }
}

The base keyword is used to refer to the object. By default the override method will return base.ToString(). You can use this base object to call the non-overwritten method.

Now when we create an instance of our new class and call the ToString method we are given "Hello World 123456″.

Create New Account
help
Visual Studio 2005 Crystal Report I am using visual studio 2005 with mysql database. I have created few report by using Visual Studio 2005 Crystal Report Tools, however, i keep facing time out request when loading the report report? A very good sample: To start with Reports, you need start .NET 2005 Development Studio. Start a new ASP .NET Web site, Choose Location as "File System" and provide a Connection string strConn = ConfigurationManager.AppSettings["connectionstring"]; SqlConnection sqlConn = new SqlConnection(strConn); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(strCmd, sqlConn); / / - -this statement is very important, here the table name should
visual studio light switch application in 3 tier. Hi to All, how develop a visual studio light switch application in 3 tier. (using bal and dal). could u plz send any public static class DataLayer { Public static DataSet getdata(string query) { SqlConnection con = new SqlConnection("constring"); SqlDataAdapter da; string mySQL = query; da = new SqlDataAdapter(mySQL, con); con.Open(); DataSet ds = new DataSet(); da.Fill(ds); Return ds; } } note - this
visual studio light switch application in 3 tier. Hi to All, Am doing a app using visual studio light switch. in this how to write and get a method from bal file. could any one. Please refer your prvious post http: / / www.eggheadcafe.com / community / xaml / 56 / 10372230 / visual-studio-light-switch-application-in-3-tier.aspx Untitled document Presentation Layer (UI) Presentation layer cotains connstring" ].ToString(); public DAL() { / / / / TODO: Add constructor logic here / / } public static DataTable getdata( int empid) { SqlDataAdapter da = new SqlDataAdapter ( "select * from table1 where empid = " + empid, connStr); DataTable dt = new DataTable (); da.Fill(dt); return
AJAX :: Visual Studio 2010 And Autocomplete Extender with database AJAX :: Visual Studio 2010 And Autocomplete Extender with database Make use of Ajax AutoComplete Extender <asp:ScriptManager ID CompletionListItemCssClass = "autocomplete_listItem" CompletionListHighlightedItemCssClass = "autocomplete_highlightedListItem" DelimiterCharacters = ";, :" ShowOnlyCurrentWordInCompletionListItem = "true" > < / ajaxToolkit:AutoCompleteExtender > hope this will help you AJAX :: Visual Studio 2010 And Autocomplete Extender with database write a webmethod ‘GetCountryInfo’ to fetch the data from GetCountryInfo(string prefixText) { int count = 10; string sql = "Select * from Country Where Country_Name like @prefixText"; SqlDataAdapter da = new SqlDataAdapter(sql, ”Your Connection String Comes Here”)); da.SelectCommand.Parameters. Add("@prefixText", SqlDbType.VarChar, 50).Value
How to ad crystal report in Visual Studio 2008 When I go to Add new Item . . . . I cant view the crystal report option. . . . how to get it . . ?. . . . I am having visual studio 2008 Follow the steps: ► Add a Crystal report to the project and name it as by column, if you want. Now .CS Code: void bind() { string q = "selete * from table1" ; SqlDataAdapter da = new SqlDataAdapter (q, "ConnectionString" ); DataTable dt = new DataTable (); da.Fill(dt); CrystalReport1 cr = new CrystalReport1(); cr.SetDataSource new item Check that Crystal Reports appears in the Add New Item dialog box in Visual Studio. 1.Launch Visual Studio. 2.Create a new Web or Windows project (in any language