VB .NET到C#.NET代码转换器 [英] vb .net to c# .net code converter

查看:57
本文介绍了VB .NET到C#.NET代码转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,

我想将下面的代码转换为c#.net.但是发生错误.plz对其进行纠正.
在下面的代码中我无法转换为c#.下面的行显示错误
如果Page.IsPostBack = False,那么
BindGrid()"
以及行"和"Gridview1.datasource"也显示错误.

Hai,

I want to convert the below code into c# .net.But the error occurs.plz rectify it.
In the below code i can''t get converted to c#.Below line shows error
"If Page.IsPostBack = False Then
BindGrid()"
and also "Row" and "Gridview1.datasource" also showing error.

Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            BindGrid()
        End If
    End Sub

    Sub BindGrid()
        Dim oDs As New DataSet
        oDs.ReadXml(Request.PhysicalApplicationPath + "XMLFile.xml")
        GridView1.DataSource = oDs
        GridView1.DataBind()
    End Sub

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        BindGrid()
    End Sub

    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        BindGrid()
    End Sub

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        BindGrid()
        Dim oDs As DataSet = GridView1.DataSource
        oDs.Tables(0).Rows(GridView1.Rows(e.RowIndex).DataItemIndex).Delete()
        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml")
        BindGrid()
    End Sub

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        BindGrid()
    End Sub

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim i As Integer = GridView1.Rows(e.RowIndex).DataItemIndex
        Dim strId As String = CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
        Dim strName As String = CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
        Dim strTel As String = CType(GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text

        GridView1.EditIndex = -1
        BindGrid()
        Dim oDs As DataSet = GridView1.DataSource

        oDs.Tables(0).Rows(i).Item(0) = strId
        oDs.Tables(0).Rows(i).Item(1) = strName
        oDs.Tables(0).Rows(i).Item(2) = strTel

        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml")
        BindGrid()

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        BindGrid()
        Dim oDs As DataSet = GridView1.DataSource
        Dim oDr As DataRow = oDs.Tables(0).NewRow
        oDr("id") = txtId.Text
        oDr("name") = txtName.Text
        oDr("phone") = txtTel.Text
        oDs.Tables(0).Rows.Add(oDr)
        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml")
        BindGrid()
    End Sub
End Class

推荐答案

尝试以下代码.应该可以.
OriginalGriff
提供的上述链接转换后的代码中只有2个问题
1.在后面的代码中访问GridView行/单元格应使用方括号(即"[."])
2. GridView源应解析为DataSet. [我不确定它是否会起作用:(]

Try out below code. It should work.
Only 2 issues in code converted from above link provided by OriginalGriff

1. GridView row/cell accessing in code behind should be with square brackets(i.e. "[". "]")
2. GridView source should be parse to DataSet. [ I am not sure if it will work or not :( ]

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web.UI.WebControls;
partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    public void BindGrid()
    {
        DataSet oDs = new DataSet();
        oDs.ReadXml(Request.PhysicalApplicationPath + "XMLFile.xml");
        GridView1.DataSource = oDs;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGrid();
    }
    protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        BindGrid();
        DataSet oDs = (DataSet)GridView1.DataSource;
        oDs.Tables[0].Rows[GridView1.Rows[e.RowIndex].DataItemIndex].Delete();
        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml");
        BindGrid();
    }
    protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGrid();
    }
    protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        int i = GridView1.Rows[e.RowIndex].DataItemIndex;
        string strId = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string strName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        string strTel = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        GridView1.EditIndex = -1;
        BindGrid();
        DataSet oDs = (DataSet)GridView1.DataSource;
        oDs.Tables[0].Rows[i][0] = strId;
        oDs.Tables[0].Rows[i][1] = strName;
        oDs.Tables[0].Rows[i][2] = strTel;
        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml");
        BindGrid();
    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        BindGrid();
        DataSet oDs = (DataSet)GridView1.DataSource;
        DataRow oDr = oDs.Tables[0].NewRow();
        oDr["id"] = txtId.Text;
        oDr["name"] = txtName.Text;
        oDr["phone"] = txtTel.Text;
        oDs.Tables[0].Rows.Add(oDr);
        oDs.WriteXml(Request.PhysicalApplicationPath + "XMLFile.xml");
        BindGrid();
    }
   
}



希望对您有所帮助.



Hope it helps.


我不确定您的问题是什么,我看不到代码有什么大错误.
在VB和C#之间有一个在线转换器: http://www.developerfusion.com/tools/convert/vb-to-csharp/ [ ^ ]-我经常使用它!
I''m not sure what your problem is, I can''t see anything massively wrong with the code.
There is an on-line converter between VB and C# here: http://www.developerfusion.com/tools/convert/vb-to-csharp/[^] - I use it quite a bit!


这篇关于VB .NET到C#.NET代码转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆