【问题标题】:Easiest way to label rows in a datagrid在数据网格中标记行的最简单方法
【发布时间】:2012-04-20 22:55:39
【问题描述】:

我有一个读取 xml WebResponse 的数据集,然后我从该数据集中提取和合并表我如何将 7 个硬编码标签添加到合并表中以显示在 datagrid 中,标签始终是相同的值和顺序将始终相同。

我想为了方便起见,我想在表格中添加 7 行标题?与数据网格一起显示。

我尝试了什么

 WebRequest req = WebRequest.Create(m_uri);
        WebResponse rsp;
        req.Method = "POST";
        req.ContentType = "text/xml";
        StreamWriter writer = new StreamWriter(req.GetRequestStream());
        writer.WriteLine(buildXml().ToString());
        writer.Close();
        rsp = req.GetResponse();

        //Get the Posted Data
        StreamReader reader = new StreamReader(rsp.GetResponseStream());
        string rawXml = reader.ReadToEnd();
        XmlDocument xml = new XmlDocument();
        xml.LoadXml(rawXml);

        //Use XPath to Navigate the Document
        int status = Convert.ToInt32(xml.SelectSingleNode(@"/RatingServiceSelectionResponse/Response/ResponseStatusCode").InnerText);
        if (status == 1)
        {




            StringReader srxml = new StringReader(rawXml);

            DataSet dsAuthors = new DataSet("RatedShipment");
            dsAuthors.ReadXml(srxml);

            dsAuthors.EnforceConstraints = false;

            DataTable Shipment = dsAuthors.Tables[2];
            DataTable TotalCharges = dsAuthors.Tables[8];
            DataTable table = new DataTable("UPS Service");

            Shipment.Merge(TotalCharges);
            Shipment.Columns.Remove("RatedShipmentWarning");
            Shipment.Columns.Remove("CurrencyCode");
            Shipment.Rows.RemoveAt(7);
            DataColumn UPSService = new DataColumn();
            UPSService.DataType = typeof(string); 
            UPSService.ColumnName = "UPS Service";
            Shipment.Columns.Add(UPSService);
            Shipment.Rows[0].Cells[0].Value = "UPS Ground";
            Shipment.Rows[1].Cells[0].Value = "UPS Three Day";
            Shipment.Rows[2].Cells[0].Value = "UPS Second Day A.M.";
            Shipment.Rows[3].Cells[0].Value = "UPS Second Day";
            Shipment.Rows[6].Cells[0].Value = "UPS Next Day Air Saver";
            Shipment.Rows[4].Cells[0].Value = "UPS Next Day Air Early A.M.";
            Shipment.Rows[5].Cells[0].Value = "UPS Next Day Air";



        dataGridView1.DataSource = Shipment;


        }
        else
        {
            MessageBox.Show("Unable To Process: Please Check All Fields Are Entered");
        }

但行后的单元格不正确。

【问题讨论】:

  • 你的应用是asp.net还是windows-forms应用?

标签: c# xml datagrid dataset datagridrowheader


【解决方案1】:

那么你必须这样做:

foreach (DataGridViewRow row in datagrid.Rows) {
    row.Cells(0).Value = "yourtext";
}

【讨论】:

  • 对于行标题?从上到下?
  • 你的意思是你需要重命名网格的标题?你说的是哪个标题?
  • 我需要每行中的第一个单元格是特定的,所以第 1 行第 2 行第 3 行第 4 行从上到下解释每行是什么。
  • 好的,我将 (0) 更改为 [0] 并在 datagridview 属性中添加了一列。现在我得到了我需要的所有信息,但是当我对任何列进行排序时,我添加的所有值都会从网格中删除,而不是与其余行进行排序?
  • 对不起 [] 我正在使用记事本 :),至于排序我想这对你来说是个问题,因为你在第一行的数据是静态的,我认为你应该添加静态然后为每一行记录数据库记录中的值。
猜你喜欢
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 2014-03-30
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
相关资源
最近更新 更多