2

C#のdatagridviewコントロールに自動番号列を挿入しようとしています。

すべてうまくいきますが、列に何も書き込むことができません。空のままです。

どうすればこれを修正できますか?

以下は私のコードです(grv-gridview、ds-dataset):

        grv1.AutoGenerateColumns = false;
        grv1.DataSource = ds.Tables[0];        
        grv1.Columns[0].DataPropertyName = "LastName";


        DataGridViewTextBoxColumn nrCol = new DataGridViewTextBoxColumn();
        nrCol.Name = "Nr";
        nrCol.HeaderText = "Nr";
        grv.Columns.Insert(0, nrCol);
        grv.Columns[0].ReadOnly = false;


        for (int i=0;i<grv.Rows.Count;i++)
        {
            grv.Rows[i].Cells[0].Value = i.ToString();       
        }
4

4 に答える 4

2

グリッドに行がありますか? 列を追加するコードと、存在するすべての行のフィールドを変更するループが表示されますが、行を追加するコードは表示されません。

ループ内でgrv.Rows.Add(...)事前に入力された状態で呼び出す必要があります。DataGridViewRow

于 2013-02-12T16:01:51.257 に答える
1

データソースをバインドした後find the controlassign the value to the Text property of TextBox control (セルではなく)する必要があると思います。TextBox がセルの値を覆っているため、セルの値が表示されない場合があります...

//your code here
...
//assign data source (if you have any)
grv.DataSource = YourDataSource;
//bind the gridview
grv.DataBind();

//now you can loop through the gridview as below
for (int i=0;i<grv.Rows.Count;i++)
{
    TextBox txt = (TextBox)grv.Rows[i].FindControl("Nr");
    txt.Text = i.ToString();
}

または、以下のようにこれをきちんと行うことができます。

コードビハインド:

//define index variable in the code behind
protected int index = 1;

HTML:

<!--Add this column to the GridView with your text box -->
<asp:TemplateField>
   <ItemTemplate>
       <asp:TextBox runat="server" ID="Nr" text='<%# index ++ %>'></asp:TextBox>
   </ItemTemplate>
</asp:TemplateField>
于 2013-02-12T16:04:26.073 に答える
0

私が正しければ、Person Table などがあります。その方法でそのテーブルの を作成する必要はありません。データベースからそのテーブルのとをautonumber設定するだけで完了です。primary keyauto-increment = true

アップデート

あなたのコメントを読んでください。これはあなたの問題に対する良い解決策です。ROW_NUMBER .

于 2013-02-12T22:34:48.023 に答える