C# 在datagrid中标记行的最简单方法

C# 在datagrid中标记行的最简单方法,c#,xml,datagrid,dataset,datagridrowheader,C#,Xml,Datagrid,Dataset,Datagridrowheader,我有一个在xml WebResponse中读取的数据集,然后从该数据集中提取并合并表如何向合并表中添加7个硬编码标签以显示在datagrid中,标签的值始终相同,顺序始终相同 我想为了方便起见,我想在表中添加7行标题?要与数据网格一起显示 我试过的 WebRequest req = WebRequest.Create(m_uri); WebResponse rsp; req.Method = "POST"; req.ContentType = "

我有一个在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");
        }

但行后的单元格不正确

那么你必须这样做:

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

您的应用程序是asp.net还是windows窗体应用程序?用于行标题?从上到下?你的意思是你需要重命名网格的标题?您正在谈论哪些标题?我需要每行中的第一个单元格是一个特定的单元格,因此第1行第2行第3行第4行从上到下解释每行是什么。好的,我将(0)更改为[0],并在datagridview属性中添加了一列。现在我得到了我需要的所有信息,但当我对任何列进行排序时,我添加的所有值都会从网格中删除,而不是与其余行一起排序?很抱歉[]我正在使用记事本:),至于排序,我想这将是一个问题,因为第一行中的数据是静态的,我认为应该在数据库记录中为每一行添加静态值。