Check this sample code
public DataTable ConvertDvToDt(int division)
{
DataView dv = _mtc.GetMatchResults(division);
DataTable dt = new DataTable();
// dt.Columns.Add("id", Type.GetType("System.String")).AutoIncrement.ToString();
dt.Columns.Add("MatchId", Type.GetType("System.String"));
dt.Columns.Add("MatchDate", Type.GetType("System.String"));
dt.Columns.Add("TeamName", Type.GetType("System.String"));
dt.Columns.Add("PlayerName", Type.GetType("System.String"));
dt.Columns.Add("TeamOneScore", Type.GetType("System.String"));
dt.Columns.Add("UserId", Type.GetType("System.String"));
dt.Columns.Add("LoginUserId", Type.GetType("System.String"));
dt.Columns.Add("TeamId", Type.GetType("System.String"));
DataColumn[] myPrimaryKeyColumn = new DataColumn[1];
myPrimaryKeyColumn.SetValue(dt.Columns[0], 0);
dt.PrimaryKey = myPrimaryKeyColumn;
DataTable dtSetAndScore = dv.ToTable(true, "MatchId", "TeamId", "SetNo", "Score");
DataTable dtTeam = dv.ToTable(true, "MatchId", "TeamName", "TeamId");
DataTable dtPlayer = dv.ToTable(true, "MatchId", "Name");
DataTable dtDate = dv.ToTable(true, "MatchId", "MatchDate", "UserId");
for (int i = 0; i <= dtTeam.Rows.Count - 1; i++)
{
DataRow row = dt.NewRow();
int Matchid = Convert.ToInt32(dtTeam.Rows[i]["MatchId"]);
//row = dt.NewRow();
DataRow[] drSelect = dtTeam.Select("MatchId=" + Matchid);
string temp = drSelect[0]["TeamName"].ToString();
string temp1 = drSelect[0]["TeamId"].ToString();
for (int y = 1; drSelect.Length > y; y++)
{
temp = temp + "<br/>" + drSelect[y]["TeamName"].ToString();
temp1 = temp1 + "<br/>" + drSelect[y]["TeamId"].ToString();
}
DataRow dr = dt.Rows.Find(Matchid);
if (dr == null)
{
row["MatchId"] = Matchid;
row["TeamName"] = temp;
row["TeamId"] = temp1;
dt.Rows.Add(row);
}
}
DataView dvPlayerName = new DataView();
dvPlayerName = dtPlayer.DefaultView;
DataTable dtName = dvPlayerName.ToTable();
dtPlayer = dvPlayerName.ToTable(true, "MatchId");
for (int k = 0; k <= dtPlayer.Rows.Count - 1; k++)
{
DataRow row = dt.NewRow();
int Matchid = Convert.ToInt32(dtPlayer.Rows[k]["MatchId"]);
DataRow[] drPlayerSelect = dtName.Select("MatchId=" + Matchid);
string PlayerName = drPlayerSelect[0]["Name"].ToString();
for (int y = 0; drPlayerSelect.Length > y; y++)
{
if (y == 1)
{
PlayerName = PlayerName + " / " + drPlayerSelect[y]["Name"];
}
else if (y == 2)
{
PlayerName = PlayerName + "<br/>" + drPlayerSelect[y]["Name"];
}
else if (y == 3)
{
PlayerName = PlayerName + " / " + drPlayerSelect[y]["Name"];
}
}
string TotalPlayerName = PlayerName.ToString();
dt.Rows[k]["MatchId"] = Matchid;
dt.Rows[k]["PlayerName"] = TotalPlayerName;
dt.AcceptChanges();
}
DataView dvSetAndScore = new DataView();
dvSetAndScore = dv;
DataTable dtScore = dvSetAndScore.ToTable("Score");
string Score = "";
DataView dvMatch = dtScore.DefaultView;
DataTable dtMatch = dvMatch.ToTable(true, "MatchId");
for (int i = 0; i <= dtMatch.Rows.Count - 1; i++)
{
DataRow row = dt.NewRow();
int Matchid = Convert.ToInt32(dtMatch.Rows[i]["MatchId"]);
DataRow[] drScore = dtSetAndScore.Select("MatchId=" + Matchid);
for (int j = 0; j <= drScore.Length - 1; j++)
{
Score = drScore[j]["Score"].ToString();
Score = Score + ",";
if (j == 2 || j == 5)
{
Score = Score.Substring(0, (Score.Length - 1));
Score = Score + "<br/>";
}
Score = Score.ToString();
dt.Rows[i]["MatchId"] = Matchid;
dt.Rows[i]["TeamOneScore"] += Score;
}
dt.AcceptChanges();
}
for (int i = 0; i <= dtDate.Rows.Count - 1; i++)
{
DataRow row = dt.NewRow();
int Matchid = Convert.ToInt32(dtDate.Rows[i]["MatchId"]);
Guid userId = (Guid)dtDate.Rows[i]["UserId"];
DataRow[] drScore = dtDate.Select("MatchId=" + Matchid);
DateTime AddedDate = Convert.ToDateTime(dtDate.Rows[i]["MatchDate"]);
dt.Rows[i]["MatchId"] = Matchid;
dt.Rows[i]["MatchDate"] = String.Format("{0:M/d/yyyy}", AddedDate);
dt.Rows[i]["UserId"] = userId;
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
MembershipUser userInfo = Membership.GetUser(HttpContext.Current.User.Identity.Name);
Guid LoginUserId = new Guid(userInfo.ProviderUserKey.ToString());
if (LoginUserId == userId)
{
//lblEdite.Visible = true;
}
else
{
//lblEdite.Visible = false;
}
dt.Rows[i]["LoginUserId"] = LoginUserId;
}
else
{
//lblEdite.Visible = false;
}
dt.AcceptChanges();
}
return dt;
}
Convert VB to C# with VBConversions,the most accurate code translation tool available. more information then visit: www.vbconversions.com
ReplyDeleteand Product Details : www.tangiblesoftwaresolutions.com/Product_Details/Instant_CSharp.html
vb.net to c# converter
convert vb c#
vb to c# converter