wxPython使用while循环向网格添加列表

时间:2016-05-08 13:51:20

标签: python wxpython

我试图将一个列表输入到网格中,每当我运行它时,我都会收到此错误:SetValue():wxGridStringTable中的行或列索引无效。我很抱歉,因为我最近才开始学习python。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;



public partial class admin_itemediting : System.Web.UI.Page{

    protected void Page_Load(object sender, EventArgs e)
    {
        int row = 0;
        if (Request.QueryString["itemID"] != null){

            row = int.Parse(Request.QueryString["itemID"]);
        }
        else{
            Response.Redirect("itemedit.aspx");
        }




        string connectionString = WebConfigurationManager.ConnectionStrings
            ["ConnectionString"].ConnectionString;

        SqlConnection myConnection = new SqlConnection(connectionString);


        myConnection.Open();


        string query = "SELECT * FROM reports WHERE ID=@rowid";


        SqlCommand myCommand = new SqlCommand(query, myConnection);

        myCommand.Parameters.AddWithValue("@rowid", row);


        SqlDataReader rdr = myCommand.ExecuteReader();


        while (rdr.Read())
        {
            string myname = rdr["itemname"].ToString();

            itemnametext.Text = myname;
        }
    }
    protected void updatebutton_Click(object sender, EventArgs e){

        string connectionString = WebConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString;

        SqlConnection myConnection = new SqlConnection(connectionString);

        myConnection.Open();

    string itemnametextupdate = itemnametext.Text;

    string query = "UPDATE reports SET itemname = @itemnewname";

    }













    }

}

1 个答案:

答案 0 :(得分:0)

代码正在尝试设置从第1行,第1行开始的值,但行和列基于零。 而不是使用while循环,最好使用带枚举的for循环来获取项目的索引。

import wx
import wx.grid

class main(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        items = ["milk", "cherries", "soup"]
        total = len(items)
        grid = wx.grid.Grid(self)
        grid.SetRowLabelSize(0)
        grid.SetColLabelSize(0)
        grid.CreateGrid(total, 1)
        for index, value in enumerate(items):
            grid.SetCellValue(index, 0, value)


if __name__ == "__main__":
    app = wx.App(False)
    frame = main()
    frame.Show()
    app.MainLoop()