Click here to Skip to main content
15,559,538 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a User Details in dataset1, and Dataset1 have the following fields in table, Name,Reg_Date,CityID,LocationID,LocationName,Reg_For. In code behind i need to take a userdetails depend on their location from dataset1 and filtered data's stored into dataset2.. Anyone give a suggestions plz?
Posted
Updated 8-Oct-17 3:01am
Comments
SnvMohan 4-Dec-13 1:51am    
DataRow[] rows = Dataset1.Tables[0].Select("Location_Name IN (" + Selected_locations + ")");
DataRow NewRow;

foreach (DataRow row in rows)
{
NewRow = NewTB.NewRow();
NewRow["Name"] = row["Name"];
NewRow["Reg_Date"] = row["Reg_Date"];
NewRow["Location_Name"] = row["Location_Name"];
NewRow["CityID"] = row["CityID"];
NewRow["LocationID"] = row["LocationID"];
NewRow["Reg_For"] = row["Reg_For"];
NewTB.Rows.Add(NewRow);
}

i have a more then one location for search. I can use this code..but there is a error on that select query….How to solve this?
Member 3356170 1-Jan-18 22:22pm    
DataRow[] rows = Dataset1.Tables[0].Select("Location_Name IN (" + Selected_locations + ")");
DataRow NewRow;

NewTB.Columns.Add("Name", typeof(string));
NewTB.Columns.Add("Reg_Date", typeof(DateTime));
NewTB.Columns.Add("Location_Name", typeof(string));
NewTB.Columns.Add("CityID", typeof(string));
NewTB.Columns.Add("LocationID", typeof(string));
NewTB.Columns.Add("Reg_For", typeof(string));

foreach (DataRow row in rows)
{
NewRow = NewTB.NewRow();
NewRow["Name"] = row["Name"];
NewRow["Reg_Date"] = row["Reg_Date"];
NewRow["Location_Name"] = row["Location_Name"];
NewRow["CityID"] = row["CityID"];
NewRow["LocationID"] = row["LocationID"];
NewRow["Reg_For"] = row["Reg_For"];
NewTB.Rows.Add(NewRow);
}

For Filter Data in Data Set Use Select Method for Filtering data, it Returns Datarows.
You can now add these datarows into new dataset.
 
Share this answer
 
v2
I have written a demo code for you just get the idea here, ANd check your actual data type where i have mentioned like string, datetime,int...
Just check your way
But it should like this

C#
var filterdata = (from f in dataset1.Tables[0].AsEnumerable()
                          where f.Field<string>("Location") == "India"
                          select new
                          {
                              Name = f.Field<string>("Name"),
                              Reg_Date = f.Field<DateTime>("Reg_Date"),
                              CityID = f.Field<int>("CityID"),
                              LocationID = f.Field<int>("LocationID"),
                              LocationName = f.Field<string>("LocationName"),
                              Reg_For = f.Field<string>("Reg_For")
                          }).ToList();
        foreach (var item in filterdata)
        {
            DataRow row = new DataRow();
            row["Name"] = item.Name;
            row["Reg_Date"] = item.Reg_Date;
            row["CityID"] = item.CityID;
            row["LocationID"] = item.LocationID;
            row["LocationName"] = item.LocationName;
            row["Reg_For"] = item.Reg_For;
            dataset2.Tables[0].Rows.Add(row);
        }
 
Share this answer
 
v2
This would be easy if you will use this.

C#
DataTable table1 = new DataTable("Registration");
      table1.Columns.Add("Location");
      table1.Columns.Add("Id");
      table1.Rows.Add("Loc1", 1);
      table1.Rows.Add("Loc2", 2);
      table1.Rows.Add("Loc3", 3);
      table1.Rows.Add("Loc1", 4);
      table1.Rows.Add("Loc4", 5);
      table1.Rows.Add("Loc1", 6);
      table1.Rows.Add("Loc1", 7);
      table1.Rows.Add("Loc5", 8);


      DataTable table2 = new DataTable("Registration1");
      table2.Columns.Add("Location");
      table2.Columns.Add("id");

      DataSet Dset = new DataSet("DSET1");
      DataSet Dset1 = new DataSet("DSET2");
      Dset.Tables.Add(table1);

      if (Dset.Tables[0].Rows.Count > 0)
      {
          for (int i = 0; i < Dset.Tables[0].Rows.Count; i++)
          {
              DataRow dr = Dset.Tables[0].Rows[i];
              if (dr["Location"].ToString() == "Loc1")
              {
                  table2.Rows.Add(dr["Location"].ToString(),dr["Id"].ToString());


              }
          }
          Dset1.Tables.Add(table2);
      }



Best of luck.
 
Share this answer
 
Try this
C#
var query = from C in dataSet.Table["tablename"].AsEnumerable()
                        where C["Location"].ToString() == "USA"
                        select new {Location=C.Field<string>("Location"),Name=C.Field<string>("Name")};
           
            DataTable dataTable = new DataTable();
            DataRow dr = dataTable .NewRow();
                foreach(DataRow row in query)
                {
                    dr[0] = row.ItemArray[1].ToString();
                    dr[1] = row.ItemArray[2].ToString();
                    dataTable .Rows.Add(dr);
                }</string></string>

C#
DataSet Newdataset=new DataSet();
Newdataset.Tables.Add(dataTable );
 
Share this answer
 
dim dataset2 as dataset = dataset1.Clone()
Dim rows As DataRow() = dataset1.Tables(0).[Select](" where col=123")
For Each row As DataRow In rows
 dataset2.Tables(0).Rows.Add(row)
Next
 
Share this answer
 
Comments
CHill60 8-Oct-17 11:31am    
The question was answered 4 years ago. Stick to answering new questions where the OP still needs help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900